[R] confusion with R syntax

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 11 22:26:25 CEST 2007


Or with

x <- 1:10

# these are all the same

x[2:4][1]

(x[2:4])[1]

y <- x[2:4]
y[1]

On 10/11/07, Andrew Robinson <A.Robinson at ms.unimelb.edu.au> wrote:
> 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/
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list