Reputation: 3640
I have an easy task but it's difficult for me to describe it and find it on stackoverflow.
I have three vectors
v1 <- c(1,1,1,1,1)
v2 <- c(2,2,2,2,2)
v3 <- c(3,3,3,3,3)
how can I combine them elementwise, resulting in:
c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
I know that I can get this vector by rep(c(1,2,3), 5)
, I'm looking for a generic solution for all vectors of the same length.
Upvotes: 3
Views: 89
Reputation: 1778
You could use this:
as.vector(apply(mapply(c, list(v1,v2,v3)),1,c))
Upvotes: 0
Reputation: 32548
foo = function(...){
L = list(...)
c(matrix(unlist(L), length(L), byrow = TRUE))
}
foo(v1, v2, v3)
# [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Upvotes: 2