R/paste_sans_NA.R

Defines functions paste_sans_NA

Documented in paste_sans_NA

#' wrapper function on paste() that replaces NA with ""
#'
#' wrapper function on paste() that replaces NA with ""
#' @keywords paste NA
#' @export
#' @examples
#' paste_sans_NA(c(1,2,NA), c(1,NA, NA), sep = "_")

paste_sans_NA <- function(..., sep = " ") {
  L <- list(...)
  L <- lapply(L, function(x) {x[is.na(x)] <- ""; x})
  ret <- gsub(paste0("(^", sep, "|", sep, "$)"), "",
              gsub(paste0(sep, sep),sep,
                   do.call(paste,c(L,list(sep=sep)))))
  is.na(ret) <- ret == ""
  ret
}

# h/t 42- in https://stackoverflow.com/questions/13673894/suppress-nas-in-paste
Kidapt/keda documentation built on Nov. 23, 2019, 3:35 a.m.