Loop through a vector of vectors Loop through a vector of vectors r r

Loop through a vector of vectors


Look at what vects is:

> vects [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

The c() joins (in this case) the three vectors, concatenating them into a single vector. In the for() loop, v takes on each values in vects in turn and prints it, hence the result you see.

Did you want a list of the three separate vectors? If so

> vects2 <- list(foo, bar, baz)> for(v in vects2) {print(v)}[1] 1 2 3 4 5[1]  6  7  8  9 10[1] 11 12 13 14 15

In other words, form a list of the vectors, not a combination of the vectors.


Substitute vects <- list(foo,bar,baz) for vects <- c(foo,bar,baz).

There is no such thing (really) as a vector of vectors.