Combine multiple words into a single string in R

Oxford commas are optional

R
Author

Nick Twort

Published

16 Aug 2023

If you’ve got a vector of words that you want to combine with a different separator for the last pair of words (eg, you want to use ‘and’), just use knitr::combine_words()

For example:

knitr::combine_words(c("banana", "apple", "orange"))
banana, apple, and orange

You can even ask for no Oxford comma:

knitr::combine_words(c("banana", "apple", "orange"), and = " but not ", oxford_comma = FALSE)
banana, apple but not orange