Combining strings for debugging or pretty output is a common task in every programming language. In this article, we will learn how to concatenate strings in R.
To concatenate strings in R, we can use the
paste function. By default, the
paste function will combine strings with a single space.
paste("Thor", "Loki") #>  Thor Loki
We can specify a custom separator with the
paste("Thor", "Loki", sep = ", ") #>  Thor, Loki
Finally, we can also pass a vector of strings rather than typing each string in the
names = c("Thor", "Loki") paste(names, sep = ", ") #>  Thor, Loki