R/comma_and.R

Defines functions comma_and

Documented in comma_and

#' @export
#' @title Add commas and 'and'
#' @description Given a character vector, add commas and 'and'.
#' @param x a character vector
comma_and <- function(x) {
  y <- length(x)
  if(y<2) return(x)
  if(y==2) return(paste0(x[1], ' and ', x[2]))
  else paste0(paste0(x[1:(y-1)], collapse=', '), ', and ', x[y])
}
capellett/scutils documentation built on Oct. 20, 2023, 5:38 p.m.