R/nmr_exclude_region.R

Defines functions nmr_exclude_region.nmr_dataset_1D nmr_exclude_region

Documented in nmr_exclude_region nmr_exclude_region.nmr_dataset_1D

#' Exclude region from samples
#'
#' Excludes a given region (for instance to remove the water peak)
#' 
#' @param samples An object
#' @param exclude A list with regions to be removed Typically:
#'                                `exclude = list(water = c(4.7, 5.0))`
#' @return The same object, with the regions excluded
#' @export
#' @examples 
#' nmr_dataset <- nmr_dataset_load(system.file("extdata", "nmr_dataset.rds", package = "AlpsNMR"))
#' exclude_regions <- list(water = c(5.1, 4.5))
#' nmr_dataset <- nmr_exclude_region(nmr_dataset, exclude = exclude_regions)
#' 
nmr_exclude_region <- function(samples, exclude = list(water = c(4.7, 5.0))) {
    UseMethod("nmr_exclude_region")
}

#' @rdname nmr_exclude_region
#' @family nmr_dataset_1D functions
#' @export
#' @examples 
#' nmr_dataset <- nmr_dataset_load(system.file("extdata", "nmr_dataset.rds", package = "AlpsNMR"))
#' exclude_regions <- list(water = c(5.1, 4.5))
#' nmr_dataset <- nmr_exclude_region(nmr_dataset, exclude = exclude_regions)
#' 
nmr_exclude_region.nmr_dataset_1D <- function(samples, exclude = list(water = c(4.7, 5.0))) {
    if (is.null(exclude) || length(exclude) == 0) {
        return(samples)
    }
    axis_include <- rep(TRUE, length(samples[["axis"]]))
    for (i_region in seq_along(exclude)) {
        region <- exclude[[i_region]]
        excl_dim1 <- samples[["axis"]] >= min(region) & samples[["axis"]] <= max(region)
        axis_include[excl_dim1] <- FALSE
    }
    samples[["axis"]] <- samples[["axis"]][axis_include]
    samples[["data_1r"]] <- samples[["data_1r"]][, axis_include]
    return(samples)
}

Try the AlpsNMR package in your browser

Any scripts or data that you put into this service are public.

AlpsNMR documentation built on April 1, 2021, 6:02 p.m.