1

I have a list containing 100 lists within it, each of which has 552 numerical values. How do I sequentially extract the 1st value (and so on up to 552) from each of the 100 lists?

Example: 5 lists within a list containing the numbers 1-10

list(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(1, 2, 3, 4, 5, 6, 7, 
8, 9, 10), c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10), c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))

I want to extract each term sequentially i.e. 1,1,1,1,1 and then 2,2,2,2,2 and so on

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Kvothe
  • 79
  • 8

1 Answers1

0

This statement produces a list of vectors, taking the first element of each of your original vectors, the second element, etc., giving NA for the value of a short vector:

num <-  max(unlist(lapply(x, length)))  ## Length of the longest vector in x

lapply(seq(num), function(i) unlist(lapply(x, `[`, i)))

And here's a matrix approach:

matrix(unlist(x), ncol=length(x))

The rows of that matrix are your elements. This relies on each vector being the same length.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • Cheers Matthew - I was being supremely daft; converting to a matrix solved all my problems!!! – Kvothe Jan 20 '14 at 15:19