[R] confusion with R syntax

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Thu Oct 11 22:12:32 CEST 2007


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



More information about the R-help mailing list