[R] List indexing question

Michael Sumner mdsumner at gmail.com
Tue May 22 02:22:30 CEST 2012


There is also the recursive extraction with "[[":

x[[c(2, 1)]]
[1] 4

>From ?Extract

" ‘[[’ can be applied recursively to lists, so that if the single
     index ‘i’ is a vector of length ‘p’, ‘alist[[i]]’ is equivalent to
     ‘alist[[i1]]...[[ip]]’ providing all but the final indexing
     results in a list. "

On Tue, May 22, 2012 at 10:14 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> It's a little funny, you actually need
>
> x[[2]][1]
>
> What's going on is the following:
>
> lists can contain anything else in R, including more lists so
> subsetting them takes a hair more work. x[2] returns the sublist of x
> containing the second list element -- this is, however, not the same
> as x[[2]] which truly returns the second element of x. The single
> brackets allow more complex subsetting: x[1:2] would return the
> sublist of first and second elements (still in a list) while x[[1:2]]
> would be an error (because that doesn't really make any sense)
>
> The way I heard this explained best is: if x is a train, x[2] is the
> second car of the train, while x[[2]] is the contents of that car.
>
> Hope this helps,
> Michael
>
> On Mon, May 21, 2012 at 8:07 PM, David Perlman <dperlman at wisc.edu> wrote:
>> Consider the following:
>>> x<-list(c(1,2,3),c(4,5,6))
>>> x[1]
>> [[1]]
>> [1] 1 2 3
>>
>>> x[2]
>> [[1]]
>> [1] 4 5 6
>>
>> So far that all seems reasonable.  But now there's a problem.  I'm used to python, where I would say x[2][1] and get the value 4.  But I can't figure out how to do that in R.
>>
>>> x[2][1]
>> [[1]]
>> [1] 4 5 6
>>
>>> x[2,1]
>> Error in x[2, 1] : incorrect number of dimensions
>>
>> I have no idea why x[2][1] returns the same thing as x[2]; that makes no sense to me at all.
>>
>> What is the proper syntax for what I'm trying to do?
>>
>> Thanks!
>>
>>
>> -dave----------------------------------------------------------------------
>> A neuroscientist is at the video arcade, when someone makes him a $1000 bet
>> on Pac-Man. He smiles, gets out his screwdriver and takes apart the Pac-Man
>> game. Everyone says "What are you doing?" The neuroscientist says "Well,
>> since we all know that Pac-Man is based on electric signals traveling
>> through these circuits, obviously I can understand it better than the other
>> guy by going straight to the source!"
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list