How can I concatenate (merge, combine) two values? For example I have:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
My goal is to concatenate the two values in "tmp" to one string:
tmp_new = "GAD,AB"
Which function can do this for me?
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"
Hope this helps.