rose
rose

Reputation: 1991

Create a vector from the elements of other vectors

I have 4 vectors a, b, c and d. I would like to create a vector v which in the first iteration i want to use the first element of a, b, c and d and in the second I want to create the v by the second element of a, b, c and d , and so on. How can I do this in R? for example:

     a = c(1, 3, 6, 7)
     b = c(2, 4, 6, 8)
     c = c(4, 6, 8, 9)
     d = c(-1, 3, 6, -3)

and the end for example I should have 4 different v vectors.

Upvotes: 1

Views: 897

Answers (2)

Erich Studerus
Erich Studerus

Reputation: 557

Another solution is:

m <- matrix(c(a, b, c, d), nrow = length(a))

Then index by row to get the desired vectors, for example, m[1,]

Upvotes: 0

Ricardo Saporta
Ricardo Saporta

Reputation: 55420

   V <- mapply(FUN=c, a, b, c, d, SIMPLIFY=FALSE)

To access the results, you'd use V[[1]], V[[2]] etc.

Upvotes: 2

Related Questions