[R] confusion with R syntax
Giovanni Petris
GPetris at uark.edu
Fri Oct 12 00:15:11 CEST 2007
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/
More information about the R-help
mailing list