I just noticed something by accident with R syntax that I'm sure is correct but I don't understand it. If I have a simple numeric vector x and I subscript it, it seems that I can then subscript a second time with TRUE or FALSE, sort of like a 2 dimensional array in C. Does someone know if this is documented somewhere Because it's neat but I never knew it existed. To me it seems like a 1 dimensional vector should have only one dimensional indexing ? x <- seq(1,10)> x[1] 1 2 3 4 5 6 7 8 9 10> x[2:4][c(TRUE,FALSE,TRUE)][1] 2 4 But, it only works for TRUE or FALSE and not numbers so I think it's not really 2 dimensional indexing. x[1][2] [1] NA If someone could explain this mechanism or tell me what I should look for in the archives, it would be appreciated. Thanks. -------------------------------------------------------- This is not an offer (or solicitation of an offer) to bu...{{dropped:22}}
Hi Mark, what's happening here is that R is applying the one-dimensional subscripting operations sequentially. Try x <- seq(1,10) x[2:4][1] Cheers Andrew On Thu, Oct 11, 2007 at 03:34:22PM -0400, Leeds, Mark (IED) wrote:> I just noticed something by accident with R syntax that I'm sure is > correct but I don't understand it. If I have > a simple numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE > or FALSE, sort of like a 2 dimensional array in C. Does someone know if > this is documented somewhere > Because it's neat but I never knew it existed. To me it seems like a 1 > dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > > x > [1] 1 2 3 4 5 6 7 8 9 10 > > x[2:4][c(TRUE,FALSE,TRUE)] > [1] 2 4 > > But, it only works for TRUE or FALSE and not numbers so I think it's not > really 2 dimensional indexing. > > x[1][2] > > [1] NA > > If someone could explain this mechanism or tell me what I should look > for in the archives, it would > be appreciated. Thanks. > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to bu...{{dropped:22}} > > ______________________________________________ > 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.-- Andrew Robinson Department of Mathematics and Statistics Tel: +61-3-8344-9763 University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr http://blogs.mbs.edu/fishing-in-the-bay/
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Leeds, Mark (IED) > Sent: Thursday, October 11, 2007 12:34 PM > To: r-help at stat.math.ethz.ch > Subject: [R] confusion with R syntax > > I just noticed something by accident with R syntax that I'm sure is > correct but I don't understand it. If I have > a simple numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE > or FALSE, sort of like a 2 dimensional array in C. Does > someone know if > this is documented somewhere > Because it's neat but I never knew it existed. To me it seems like a 1 > dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > > x > [1] 1 2 3 4 5 6 7 8 9 10 > > x[2:4][c(TRUE,FALSE,TRUE)] > [1] 2 4 > > But, it only works for TRUE or FALSE and not numbers so I > think it's not > really 2 dimensional indexing. > > x[1][2] > > [1] NA > > If someone could explain this mechanism or tell me what I should look > for in the archives, it would > be appreciated. Thanks.Mark, Indexing with numbers works the same as with logicals. The problem with your example> x[1][2]is that x[1] returns a single value, but your are then asking for the second value which is NA. Try the following>x[2:4][2]Hope this is helpful, Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204
On 11/10/2007, Leeds, Mark (IED) <Mark.Leeds at morganstanley.com> wrote:> I just noticed something by accident with R syntax that I'm sure is > correct but I don't understand it. If I have > a simple numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE > or FALSE, sort of like a 2 dimensional array in C. Does someone know if > this is documented somewhere > Because it's neat but I never knew it existed. To me it seems like a 1 > dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > > x > [1] 1 2 3 4 5 6 7 8 9 10 > > x[2:4][c(TRUE,FALSE,TRUE)] > [1] 2 4 > > But, it only works for TRUE or FALSE and not numbers so I think it's not > really 2 dimensional indexing. > > x[1][2] > > [1] NA > > If someone could explain this mechanism or tell me what I should look > for in the archives, it would > be appreciated. Thanks.I may be being naive, but it is much simply than you are trying to make it. Aren't the two indexing operations run in sequence? Therefore x[2:4][c(TRUE,FALSE,TRUE)] actually is interpreted in two steps. Try this: x <- seq(1,10) y <- x[2:4] z <- y[c(TRUE, FALSE, TRUE)] Indeed, while your code: x[1][2] doesn't work, if you try x[2:4][3] then you get the third index of the vector created by 2:4 ie y <- x[2:4] z <- y[3] Best wishes, Mark -- Dr. Mark Wardle Specialist registrar, Neurology Cardiff, UK
Leeds, Mark (IED) wrote:> I just noticed something by accident with R syntax that I'm sure is > correct but I don't understand it. If I have > a simple numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE > or FALSE, sort of like a 2 dimensional array in C. Does someone know if > this is documented somewhere > Because it's neat but I never knew it existed. To me it seems like a 1 > dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > >>x > > [1] 1 2 3 4 5 6 7 8 9 10 > >>x[2:4][c(TRUE,FALSE,TRUE)] > > [1] 2 4 > > But, it only works for TRUE or FALSE and not numbers so I think it's not > really 2 dimensional indexing. > > x[1][2] > > [1] NA > > If someone could explain this mechanism or tell me what I should look > for in the archives, it would > be appreciated. Thanks.x<-1:10 x1<-x[2:4] x1 x1[c(TRUE,FALSE,TRUE)] The first extraction is performed before the second. Jim
It is not 2 dimensional indexing, but 1 dimensional indexing done twice. Would this surprise you?:> x <- 1:10 > y <- x[2:4] > y[ c(TRUE,FALSE,TRUE) ][1] 2 4 Because your code is just the same (skipping the intermediate y step). You start with x, then apply a subscripting argument to that grabbing the 2nd, 3rd, and 4th elements. The result is a vector of length 3 you then apply a second subscripting to the new vector which returns the 1st and 3rd elements of this vector (which happen to be the 2nd and 4th of the original vector). Your second example does not work because the result of the 1st subscripting is a vector of length 1, you then ask for the 2nd element. Try: x[ c(TRUE,FALSE) ][ c(1,3) ] Chaining the subscripting can be powerful, but can also lead to deep magic> x$y[[1]][,3][z==1][c(FALSE,TRUE)]This starts with a list x and gives you the element of x named y (which is also a list), then gives you the first element in y (which is a matrix or data frame), then it gives you the 3rd column of that (now down to a vector), then it gives you those elements of that vector corresponding to another vector, z, equalling 1, then finally gives you the even elements of that result. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Leeds, Mark (IED) > Sent: Thursday, October 11, 2007 1:34 PM > To: r-help at stat.math.ethz.ch > Subject: [R] confusion with R syntax > > I just noticed something by accident with R syntax that I'm > sure is correct but I don't understand it. If I have a simple > numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE or FALSE, sort of like a 2 > dimensional array in C. Does someone know if this is > documented somewhere Because it's neat but I never knew it > existed. To me it seems like a 1 dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > > x > [1] 1 2 3 4 5 6 7 8 9 10 > > x[2:4][c(TRUE,FALSE,TRUE)] > [1] 2 4 > > But, it only works for TRUE or FALSE and not numbers so I > think it's not really 2 dimensional indexing. > > x[1][2] > > [1] NA > > If someone could explain this mechanism or tell me what I > should look for in the archives, it would be appreciated. Thanks. > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to > bu...{{dropped:22}} > > ______________________________________________ > 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. >
Mark, That has to do with how operators bind and their precedence.> Date: Thu, 11 Oct 2007 15:34:22 -0400 > From: "Leeds, Mark (IED)" <Mark.Leeds at morganstanley.com> > Sender: r-help-bounces at r-project.org > Importance: normal > Priority: normal > Precedence: list > Thread-topic: confusion with R syntax > Thread-index: AcgMPbmc7chsZtPAT+K0aJ+6a2FyxQ=> > I just noticed something by accident with R syntax that I'm sure is > correct but I don't understand it. If I have > a simple numeric vector x and I subscript it, it seems that I can then > subscript a second time with TRUE > or FALSE, sort of like a 2 dimensional array in C. Does someone know if > this is documented somewhere > Because it's neat but I never knew it existed. To me it seems like a 1 > dimensional vector should > have only one dimensional indexing ? > > x <- seq(1,10) > > x > [1] 1 2 3 4 5 6 7 8 9 10 > > x[2:4][c(TRUE,FALSE,TRUE)] > [1] 2 4 >This is interpreted as (x[2:4])[c(TRUE,FALSE,TRUE)]> But, it only works for TRUE or FALSE and not numbers so I think it's not > really 2 dimensional indexing. > > x[1][2] > > [1] NA >This doesn't work because you are asking for the second element of a vector with only one element. Best, Giovanni> If someone could explain this mechanism or tell me what I should look > for in the archives, it would > be appreciated. Thanks. > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to bu...{{dropped:22}} > > ______________________________________________ > 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. > >-- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/