Skip to content

Commit

Permalink
added get_overlap_depths() function
Browse files Browse the repository at this point in the history
  • Loading branch information
wfinsinger committed Jul 19, 2024
1 parent 21a7b38 commit 381608d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(SeriesDetrend)
export(arco)
export(check_pretreat)
export(cpts_ar)
export(get_overlap_depths)
export(global_thresh)
export(local_thresh)
export(mgcv2tapas)
Expand All @@ -23,12 +24,15 @@ importFrom(changepoint,cpts)
importFrom(dplyr,"%>%")
importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,count)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pick)
importFrom(dplyr,pull)
importFrom(dplyr,relocate)
importFrom(dplyr,select)
Expand Down
56 changes: 56 additions & 0 deletions R/get_overlap_samples.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Get overlapping sample depths
#'
#' This function makes it easy to see which samples in the input file have
#' overlapping sample depths. It may be used if the
#' \code{tapas::check_pretreat()} function flags a warning for the presence of
#' overlapping sample depths.
#'
#' @details
#' The function checks if any \code{CmBot[i] > CmTop[i+1]}. If \code{TRUE},
#' a data.frame with the overalpping samples is returned.
#'
#' @param series The input data frame. A matrix with the following columns:
#' \code{CmTop, CmBot, AgeTop, AgeBot, Volume},
#' and one or more columns with the data
#' that is being analysed (variables).
#'
#' @return A data frame including only the rows of the overlapping samples
#' sorted in ascending order by depth. The rownames are inherited from the
#' input data.frame, and mya thus help finding the flagged samples in raw data
#' files.
#'
#' @seealso [check_pretreat()]
#'
#' @importFrom dplyr distinct arrange pick
#'
#' @export
#'
#' @author Walter Finsinger
#'
get_overlap_depths <- function(series) {


## Gather data --------------------------------------------------------------
A <- series
cm <- A[ ,1]
cmB <- A[ ,2]


## Check if the depth scale is continuous (cmBot[i] > cmTop[i+1]) ----------

## Get difference between cmBot of sample[i] and cmTop of sample[i+1].
overlaps_index <- cmB[1:length(cmB) - 1] - cm[2:length(cm)]

# Get indices for overlapping samples
overlaps <- which(overlaps_index > 0)

# Get overlapping samples from input data.frame:
all_overlap_samples <- rbind(A[overlaps, ], A[overlaps + 1, ])

# Remove duplicates and sort in ascending order by top depth
overlap_samples <- dplyr::distinct(all_overlap_samples) %>%
dplyr::arrange(dplyr::pick(1))

# Return output
return(overlap_samples)
}
36 changes: 36 additions & 0 deletions man/get_overlap_depths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 381608d

Please sign in to comment.