[R] confusion with R syntax

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Thu Oct 11 22:02:27 CEST 2007


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/



More information about the R-help mailing list