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!"
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.
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.