[R] How to extract sublist from a list?

Joshua Wiley jwiley.psych at gmail.com
Thu Aug 4 11:23:56 CEST 2011


On Thu, Aug 4, 2011 at 2:08 AM, Ashim Kapoor <ashimkapoor at gmail.com> wrote:
>
> On Thu, Aug 4, 2011 at 2:12 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>>
>> On Thu, Aug 4, 2011 at 1:40 AM, Ashim Kapoor <ashimkapoor at gmail.com>
>> wrote:
>> >
>> >
>> > On Thu, Aug 4, 2011 at 1:02 PM, Joshua Wiley <jwiley.psych at gmail.com>
>> > wrote:
>> >>
>> >> 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.
>> >>
>> >
>> >> j<- for ( t in seq(1:2) )  lst[[t]]$y
>> >> j
>> > NULL
>> >
>> > Why is j NULL  ?
>>
>> You are confusing how for loops work, please read the documentation for
>> ?for
>>
> The help says : -
>    ‘for’, ‘while’ and ‘repeat’ return ‘NULL’ invisibly.  ‘for’ sets
>      ‘var’ to the last used element of ‘seq’, or to ‘NULL’ if it was of
>      length zero.
>
> but it does not tell me how to fix my problem which is to return the values.

sure it does, look at the Examples!  for returns null, so you need to
do the assignment on a function that actually returns what you want,
that would be: lst[[t]]$y (yep, [[]] and $ are really functions that
return values although because they are operators you may not
typically think of them like regular functions).  Of course you are
using a loop so you do not want to just keep overwriting the same
variable, so you will need to instatiate a variable outside the loop
(preferablly sized appropriately for the number of iterations in your
loop) and then do something like:

for (i in 1:2) j[[i]] <- lst[[i]]]$y

loops get to be a bit of a pain in this regard (in my opinion), which
is why I showed you several solutions that use lapply instead.  If you
have not already (hopefully you did), try them out, you'll like
them...you can basically do what you tried simply assigning the output
of lapply to a variable j without having to worry about instatiating
it and assigning to a new position each iteration, etc.

j2 <- lapply(1:2, function(i) lst[[i]]$y)

if you set up j as a list,

identical(j, j2)

ought to be TRUE.  Of course (as I showed using sapply() ), because
you are returning a single value each time, it would also be
reasonable for j to simply be a vector.

Cheers

>
>>
>> >
>> >>
>> >> ## 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/
>
> Many Thanks,
> Ashim
>



-- 
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