Last active
August 29, 2015 14:05
-
-
Save jkeirstead/48bf5845b235dc7a8d5a to your computer and use it in GitHub Desktop.
Convert a vector of values to a prose list
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
##' Converts a vector of values to a formatted prose list | |
##' | |
##' @param vals a vector of values | |
##' @param oxford a boolean indicating whether Oxford comma should be used | |
##' @param and word before ultimate entry | |
##' @return a character string | |
prose_vector <- function(vals, oxford=FALSE, and="and") { | |
if (length(vals)>1) { | |
start <- head(vals, -1) | |
start <- paste0(start, collapse=", ") | |
end <- tail(vals, 1) | |
return(paste0(start, ifelse(oxford, ", ", " "), and, " ", end)) | |
} else { | |
return(vals) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment