[R] Access comonents in lists of lists
Rolf Turner
rolf.turner at xtra.co.nz
Sat Jan 12 00:39:49 CET 2013
On 01/12/2013 12:40 AM, Bjoern Helm wrote:
> Dear R users,I have a list of equally structured lists, how can I access e.g.
> all 2nd compontents in those sub-lists?An example:lst <-
> list(rep(list(1:3),3), rep(list(4:6),3))> lst[[1]][[1]][[1]][1] 1 2
> 3[[1]][[2]][1] 1 2 3[[1]][[3]][1] 1 2 3[[2]][[2]][[1]][1] 4 5 6[[2]][[2]][1]
> 4 5 6[[2]][[3]][1] 4 5 6What I want to get are all second sub-lists, in this
> case:[[1]][[2]][1] 1 2 3and[[2]][[2]][1] 4 5 6
Your email was/is a bit hard to read .... but if I understand correctly,
what
you want is
lst2 <- lapply(lst,function(x){x[[2]]})
See ?lapply. Also associated functions like tapply() and sapply(). Very
useful gadgets. And if you really get hooked on such functionality, you
might want to have a look at the "plyr" package.
cheers,
Rolf Turner
More information about the R-help
mailing list