Ahdee
Ahdee

Reputation: 4949

Retrieving all elements in a list of list

Hi if I have a list of list like so,

List of 5
 $ 1:List of 2
  ..$ a: Named num [1:36] 3.29 3.25 3.36 3.26 3.34 ...
  .. ..- attr(*, "names")= chr [1:36] "V1" "V2" "V3" "V4" ...
  ..$ b: Named num [1:36] 0.659 0.65 0.672 0.652 0.669 ...

say its called l, is there a way I can extract all the 'a' element of the list of list? Currently I can extract a single 'a' element as such, l[[5]] [['sr']] but when I try something like l[[1:5]] [['sr']] or l[[1,5]] [['sr']] it fails. Help would greatly appreciated thanks!

Upvotes: 0

Views: 60

Answers (1)

Mark Heckmann
Mark Heckmann

Reputation: 11441

Is this what you want?

l <- list(list(a=1:3, b=1:3),
          list(a=3:1, b=3:1))
lapply(l, function(x) x[["a"]])

[[1]]
[1] 1 2 3

[[2]]
[1] 3 2 1

Upvotes: 1

Related Questions