[R] How to extract sublist from a list?

Joshua Wiley jwiley.psych at gmail.com
Thu Aug 4 09:32:05 CEST 2011


On Thu, Aug 4, 2011 at 12:12 AM, Ashim Kapoor <ashimkapoor at gmail.com> wrote:
>> How would we do this problem looping over seq(1:2) ?

Because this goes to an email list serv, it is good practice to quote
the original problem.  I have no idea what "this" is.

>>
>>
> To extend the example in the corresponding nabble post : -
>  sub1<-list(x="a",y="ab")
>  sub2<-list(x="c",y="ad")
>  lst<-list(sub1=sub1,sub2=sub2)
>  for ( t in seq(1:2) )  print(lst[[t]]$y)
>
> So I can print out the sub1$y/sub2$y but it's not clear how to extract them.

Well, to extract them, just drop the call to print. You could use them
directly in the loop or could store them in new variables.

## note seq(1:2) is redundant with simply 1:2
or (t in 1:2) print(nchar(lst[[t]]$y))

I am guess, though, that what you might be hoping to do is extract
specific elements from a list and store the extract elements in a new
list.

lapply(1:2, function(i) lst[[i]]["y"])
## or compare
lapply(1:2, function(i) lst[[i]][["y"]])

>
> My original was different though.
>
> How would  say:-
>
> for ( t in seq(1:2) ) sub"t"$y
>
> Where sub"t" evaluates to sub1 or sub 2?

if you actually want "sub1", or "sub2":

## note that I am wrapping in print() not so that it works
## but so that you can see it at the console
for (t in 1:2) print(paste("sub", t, sep = ''))

from which we can surmise that the following should work:

for (t in 1:2) print(lst[[paste("sub", t, sep = '')]])

which trivially extends to:

for (t in 1:2) print(lst[[paste("sub", t, sep = '')]]$y)

or perhaps more appropriately

for (t in 1:2) print(lst[[paste("sub", t, sep = '')]][["y"]])

If you just need to go one level down for *all* elements of your list

lapply(lst, `[[`, "y")
## or if you are only retrieving a single value
sapply(lst, `[[`, "y")

Hope this helps,

Josh

>
> Many thanks.
> Ashim
>
>
>> On Thu, Aug 4, 2011 at 10:59 AM, Richard Ma <xuanlong.ma at uts.edu.au>wrote:
>>
>>> Thank you so much GlenB!
>>>
>>> I got it done using your method.
>>>
>>> I'm just curious how did you get this idea? Cause for me, this looks so
>>> tricky....
>>>
>>> Cheers,
>>> Richard
>>>
>>> -----
>>> I'm a PhD student interested in Remote Sensing and R Programming.
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/How-to-extract-sublist-from-a-list-tp3717451p3717713.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list