R/coalesce_with_warnings.R

Defines functions coalesce_with_warnings

Documented in coalesce_with_warnings

#' A re-coding of the coalesce function to include warnings when multiple
#' values are given which are not NA and are different
#'
#' @param ... vectors of equal length and type to coalesce
#' @return vector of coalesced values
#'
#' @examples
#' coalesce_with_warnings(c(1, 2, 3), c(4, 5, 6))
#' coalesce_with_warnings(c(1, 2, 3), c(1, 2, NA))
#'
#' @export

coalesce_with_warnings <- function(...){
    d <- cbind(...)

    sapply(1:nrow(d), function(i){
        x <- d[i,]
        if(all(is.na(x))){
            out <- NA
        }
        else{
            xbar <- unique(as.vector(na.omit(x)))
            if(length(xbar) != 1){
                warning(paste0(
                    "Row ", i, " has multiple values that do not match."))
            }
            # only grab the first one
            out <- xbar[1]
        }
        out
    })
}
uclalawcovid19behindbars/behindbarstools documentation built on April 22, 2022, 4:08 a.m.