R/find_interval.R

Defines functions find_interval

Documented in find_interval

#' Find the Interval
#'
#' This function iterates through numeric vectors of points and intervals and
#' identifies for each point the interval which contains the point.
#'
#' Please see documentation for function \code{\link{point_in_interval}} for
#' details on the interval boundaries specified in argument \code{closed}.
#'
#' @param pnts numeric vector containing the points.
#' @param ints_min numeric vector containing the lower boundaries of the
#' intervals.
#' @param ints_max numeric vector containing the upper boundaries of the
#' intervals.
#' @param closed character or logical, indicating whether the interval is left-
#' or right-closed, fully closed or open.
#'
#' @return An integer vector containing the indexes of the matching intervals
#'to the points.
#'
#' @export
find_interval <- function(pnts, ints_min, ints_max, closed = "left")
{
    n_pnts <- length(pnts)
    n_ints <- unique(c(length(ints_min), length(ints_max)))
    out <- rep(NA, times = n_pnts)

    if (length(n_ints) > 1L) {
        stop("arguments 'ints_min' and 'ints_max' must have same length")
    }
    
    for (i in 1L:n_pnts) {
        for (j in 1L:n_ints) {
            test <- point_in_interval(p = pnts[i],
                                      i = c(ints_min[j], ints_max[j]),
                                      closed = closed)
            if (!is.na(test) && test) {
                out[i] <- j
            }
        }
    }

    return(out)
}
rolandseubert/DHtools documentation built on Aug. 13, 2018, 12:56 a.m.