[R] confusion with R syntax

Jim Lemon jim at bitwrit.com.au
Thu Oct 11 22:20:10 CEST 2007


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



More information about the R-help mailing list