[R] confusion with R syntax

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Thu Oct 11 22:50:58 CEST 2007


thanks to all for your explanations. It seems like, because it's done
sequentially, you can put as many [] on as 
you like as long as they make sense. It probably was a bad question but
this concept is quite different
from C which is mainly where I came from. Thanks again.

	
Mark



-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
Sent: Thursday, October 11, 2007 4:26 PM
To: Andrew Robinson
Cc: Leeds, Mark (IED); r-help at stat.math.ethz.ch
Subject: Re: [R] confusion with R syntax

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

This is not an offer (or solicitation of an offer) to bu...{{dropped:22}}



More information about the R-help mailing list