How can two strings be concatenated?

paste()

is the way to go. As the previous posters pointed out, paste can do two things:

concatenate values into one “string”, e.g.

> paste("Hello", "world", sep=" ")
[1] "Hello world"

where the argument sep specifies the character(s) to be used between the arguments to concatenate, or collapse character vectors

> x <- c("Hello", "World")
> x
[1] "Hello" "World"
> paste(x, collapse="--")
[1] "Hello--World"

where the argument collapse specifies the character(s) to be used between the elements of the vector to be collapsed.

You can even combine both:

> paste(x, "and some more", sep="|-|", collapse="--")
[1] "Hello|-|and some more--World|-|and some more"

Leave a Comment