Add "and" to character vector to be used in string contexts such as Rmarkdown documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_and <- function(x) { | |
if (!(is.character(x))) { | |
warning("`x` must be character. coercing to character") | |
x <- as.character(x) | |
} | |
lx <- length(x) | |
if (lx == 1) { | |
y <- x | |
} | |
else if (lx == 2) { | |
y <- paste(x[1], "and", x[2]) | |
} | |
else { | |
y <- c(x[1:lx-1], paste("and", x[lx])) | |
y <- paste(y, collapse = ", ") | |
} | |
return(y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment