Eric Rupley
2012-Jan-10 23:04 UTC
[R] short-hand to avoid use of length() in subsetting vectors?
Hi-- I suspect this is a frequently considered (and possibly asked) question, but I haven't thus far found an answer: For slicing a vector with x[?], is there a symbol for length(x)? I'm seeking a short-hand for referring to the length of vector one is trying to index. E.g., for a data.frame, say,> test.frame <-data.frame(matrix(c(1:100),ncol=10,nrow=10,byrow=T)) > names(test.frame) <- names(islands)[1:10]is there a short-hand for subsetting> test.frame$Baffin[1:(length(test.frame$Baffin)-3)][1] 6 16 26 36 46 56 66> >that would allow one to avoid the "(length(some.dataframe$variable)-offset)"? I was thinking something paralleling the use of negative indices in [?] might exist with seq(from,to), e.g. for the above> test.frame$Baffin[seq(,7)][1] 6 16 26 36 46 56 66>works. But the fantasy test.frame$Baffin[seq(,-3)] obviously doesn't? Any suggestions will be gratefully appreciated? As always, many thanks to the patient list members who helps on these simple questions... Best, Eric -- Eric Rupley University of Michigan, Museum of Anthropology 1109 Geddes Ave, Rm. 4013 Ann Arbor, MI 48109-1079 erupley at umich.edu +1.734.276.8572
Steve Lianoglou
2012-Jan-10 23:27 UTC
[R] short-hand to avoid use of length() in subsetting vectors?
Hi, On Tue, Jan 10, 2012 at 6:04 PM, Eric Rupley <erupley at umich.edu> wrote:> > Hi-- > > I suspect this is a frequently considered (and possibly asked) question, but I haven't thus far found an answer: > > ? ? ? ?For slicing a vector with x[?], is there a symbol for length(x)? > > I'm seeking a short-hand for referring to the length of vector one is trying to index. > > E.g., for a data.frame, say, > >> test.frame <-data.frame(matrix(c(1:100),ncol=10,nrow=10,byrow=T)) >> names(test.frame) <- names(islands)[1:10] > > is there a short-hand for subsetting > >> test.frame$Baffin[1:(length(test.frame$Baffin)-3)] > [1] ?6 16 26 36 46 56 66 >> >> > > that would allow one to avoid the "(length(some.dataframe$variable)-offset)"?Sadly there is no shorthand of the (exact) type you are looking for. You can access the data like so, however: R> head(test.frame$Baffin, -3) [1] 6 16 26 36 46 56 66 I think that's as good as you're going to get, but let's see what other suggestions pop up. -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Duncan Murdoch
2012-Jan-11 01:20 UTC
[R] short-hand to avoid use of length() in subsetting vectors?
On 12-01-10 6:04 PM, Eric Rupley wrote:> > Hi-- > > I suspect this is a frequently considered (and possibly asked) question, but I haven't thus far found an answer: > > For slicing a vector with x[?], is there a symbol for length(x)?No, there isn't. You don't need it much, but if you do you'll have to calculate it. You'll often save a noticeable amount of execution time if you store it in a local variable rather than calling length(x) repeatedly. Duncan Murdoch> > I'm seeking a short-hand for referring to the length of vector one is trying to index. > > E.g., for a data.frame, say, > >> test.frame<-data.frame(matrix(c(1:100),ncol=10,nrow=10,byrow=T)) >> names(test.frame)<- names(islands)[1:10] > > is there a short-hand for subsetting > >> test.frame$Baffin[1:(length(test.frame$Baffin)-3)] > [1] 6 16 26 36 46 56 66 >> >> > > that would allow one to avoid the "(length(some.dataframe$variable)-offset)"? > > I was thinking something paralleling the use of negative indices in [?] might exist with seq(from,to), e.g. for the above > >> test.frame$Baffin[seq(,7)] > [1] 6 16 26 36 46 56 66 >> > > works. But the fantasy > > test.frame$Baffin[seq(,-3)] > > obviously doesn't? > > Any suggestions will be gratefully appreciated? > > As always, many thanks to the patient list members who helps on these simple questions... > > Best, > Eric > > > > -- > Eric Rupley > University of Michigan, Museum of Anthropology > 1109 Geddes Ave, Rm. 4013 > Ann Arbor, MI 48109-1079 > > erupley at umich.edu > +1.734.276.8572 > > ______________________________________________ > 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.