[R] efficient list indexing

Keith Jewell k.jewell at campden.co.uk
Wed Sep 22 13:25:27 CEST 2010


Also, it may be that you want bigfred to be a list of 5 lists each of 2 
elements (happy and name) rather than a list of 10 elements.

Thus (also using double bracketing)

fred<- list(happy = 1:10, name = "squash")
bigfred <- replicate(5, fred, FALSE)
bigfred[[2]][[2]]

hth

Keith J

"Patrick Burns" <pburns at pburns.seanet.com> wrote in message 
news:4C99E118.2010805 at pburns.seanet.com...
> I'm confused by what you are looking for.
> There's some slight possibility that you
> are looking for double bracket subscripting
> with a vector:
>
> > list(a=1:5, b=letters)[[c(2,4)]]
> [1] "d"
>
>
> On 22/09/2010 10:58, Alaios wrote:
>> Hello everyone,
>>
>> I need some help with lists inside lists (a good way to emulate a 
>> struct)\
>>
>> Assume that there is a small list called fred:
>> fred<- list(happy = 1:10, name = "squash")
>>
>>
>> and a big list called bigfred that includes fred list 5 times
>> bigfred<- rep(fred,5)
>>
>> Is it possible somehow to index all these sublists(fred) inside bigfred 
>> with a
>> more direct way like this:
>> bigfred[1] shows the first sublist fred
>> bigfred[2][2] shows the second sublist fred, the second element of the 
>> fred list
>>
>> So far I found some way to do this by refering to the sublists by the 
>> following:
>> bigfred[1+index*length(fred)]
>> where index shows the beginning of a sublist.
>>
>> I would like to thank you in advance for your help
>> Best Regards
>> Alex
>>
>>
>>
>>
>> [[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.
>>
>
> -- 
> Patrick Burns
> pburns at pburns.seanet.com
> http://www.burns-stat.com
> (home of 'Some hints for the R beginner'
> and 'The R Inferno')
>



More information about the R-help mailing list