[R] List indexing question

arun smartpink111 at yahoo.com
Tue May 22 02:29:08 CEST 2012


Hi David,

Try

> x[[2]][1]
[1] 4
> 


A.K.

----- Original Message -----
From: David Perlman <dperlman at wisc.edu>
To: r-help at r-project.org
Cc: 
Sent: Monday, May 21, 2012 8:07 PM
Subject: [R] List indexing question

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.




More information about the R-help mailing list