Reputation: 12550
I am comfortable using cbind to combine two vectors as such:
a <- c(1, 2, 3, 4)
b <- c("Y", "Y", "N", "N")
cbind(a, b)
and getting this:
a b
[1,] "1" "Y"
[2,] "2" "Y"
[3,] "3" "N"
[4,] "4" "N"
But what I can't seem to find in the documentation is a way to combine the contents of a
and b
, so I can get:
a
[1,] "1Y"
[2,] "2Y"
[3,] "3N"
[4,] "4N"
What is the best way to combine the contents of 2 vectors in R?
Upvotes: 0
Views: 119
Reputation: 56905
Try paste
.
paste(1:4, letters[1:4])
# [1] "1 a" "2 b" "3 c" "4 d"
paste(1:4, letters[1:4], sep="") # same as paste0
# [1] "1a" "2b" "3c" "4d"
If you require it to be a column matrix and not a vector, you can then coerce to matrix.
matrix(paste(1:4, letters[1:4], sep=""))
# [,1]
# [1,] "1a"
# [2,] "2b"
# [3,] "3c"
# [4,] "4d"
For further information, see ?paste
.
Upvotes: 3