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")
#> [1] Thor Loki
We can specify a custom separator with the sep
option.
paste("Thor", "Loki", sep = ", ")
#> [1] Thor, Loki
Finally, we can also pass a vector of strings rather than typing each string in the paste
parameters.
names = c("Thor", "Loki")
paste(names, sep = ", ")
#> [1] Thor, Loki