Combining strings

Strings can be combined using paste, paste0, and other utilities. When using paste, the default separator is space:

paste("I","think","therefore","I","am")

We can use a different separator by using the sep flag:

paste("I","think","therefore","I","am", sep="-")

paste0 is an extension of paste where the separator is null. As a result, all strings are concatenated into a single string unless separators are specified:

paste0("I","think","therefore","I","am") 

In addition to the sep flag, there is also a collapse flag that can be used to separate the results:

paste("grade",c("A","B","C"), sep=" ") # paste with separator space paste("grade",c("A","B","C"), sep=" ", collapse=",") # paste with separator space and collapse with "," ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.