Reputation: 21
This feels like it should be easy, but I can't work it out. I have a list of lists, in the same form as below (but much, much, longer ...)
mylist <- list(list(x=c(1,2,3),y=4),
list(x=c(4,5,6,7),y=8),
list(x=c(1,2),y=6))
I want to extract a vector of the "y" values, ie c(4,8,6). Do you know how to do this? I have tried searching to no avail.
Thanks
Upvotes: 2
Views: 375
Reputation: 50728
Or using purrr
:
library(purrr)
map_dbl(mylist, "y")
#[1] 4 8 6
Upvotes: 3
Reputation: 11957
What you want is to iterate over each item of mylist
and retrieve only the "y" values. Easy with sapply
:
sapply(mylist, '[[', 'y')
[1] 4 8 6
Upvotes: 2