# [R] confusion with R syntax

Greg Snow Greg.Snow at intermountainmail.org
Thu Oct 11 22:38:15 CEST 2007

```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.
>

```