Skip to content

Instantly share code, notes, and snippets.

@jkeirstead
Last active August 29, 2015 14:05
Show Gist options
  • Save jkeirstead/48bf5845b235dc7a8d5a to your computer and use it in GitHub Desktop.
Save jkeirstead/48bf5845b235dc7a8d5a to your computer and use it in GitHub Desktop.
Convert a vector of values to a prose list
##' 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