R/pi_merge.R

Defines functions pi_merge

Documented in pi_merge

#' Merge Point and Interval Data
#'
#' This function takes two data frames with point- and interval-related data,
#' respectively, and merges them into one point-related data frame.
#'
#' Please see documentation for function \code{\link{point_in_interval}} for
#' details on the interval boundaries specified in argument \code{closed}.
#'
#' @param pd data frame containing the point data.
#' @param id data frame containing the interval data.
#' @param pnum integer or character vector of length 1 specifying the column
#' that contains the numeric values of the points.
#' @param inum_min integer of character vector of length 1 specifying the
#' column that contains the lower boundary of the intervals.
#' @param inum_max integer of character vector of length 1 specifying the
#' column that contains the upper boundary of the intervals.
#' @param pdata integer or character vector specifying the data columns to keep
#' from the point dataset (optional, defaults to all).
#' @param idata integer or character vector specifying the data columns to keep
#' from the interval dataset (optional, defaults to all).
#' @param closed character or logical, indicating whether the interval is left-
#' or right-closed, fully closed or open.
#'
#' @return A data frame containing the merged data.
#'
#' @export
pi_merge <- function(pd, id, pnum, inum_min, inum_max, pdata, idata,
                     closed = "left")
{
    if (missing(pdata)) {
        pdata <- 1L:ncol(pd)
    }
    if (missing(idata)) {
        idata <- 1L:ncol(id)
    }
    
    irows <- find_interval(pnts = pd[, pnum], ints_min = id[, inum_min],
                           ints_max = id[, inum_max], closed = closed)
    out <- cbind(pd[, pdata], id[irows, idata])

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