[R] Subset function
William Dunlap
wdunlap at tibco.com
Fri Sep 9 16:50:11 CEST 2011
I thought that the main advantage of subset() over [()
is that you only mention the name of the data.frame once,
in the first argument, not in the second:
> x <- data.frame(xin=c(1, 8, 16, 1, 8, 16), xout=c(14, 5, 884, 14, 5, 884))
> subset(x, xin > 7, select = xout) # not x$xin > 7
xout
2 5
3 884
5 5
6 884
A secondary advantage of subset is that treats NA's in
the subset= argument the same as FALSE's.
I think subset is handy for one-off usage, but in general
purpose functions the [ function is better: it uses standard
argument evaluation and is faster.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rainer
> Schuermann
> Sent: Friday, September 09, 2011 5:44 AM
> To: r-help at r-project.org; stat.kk
> Subject: Re: [R] Subset function
>
> Does that help:
>
> > x
> xin xout
> 1 1 14
> 2 8 5
> 3 16 884
> 4 1 14
> 5 8 5
> 6 16 884
>
> > subset( x, x$xin > 7, select = xout )
> xout
> 2 5
> 3 884
> 5 5
> 6 884
>
> Rgds,
> Rainer
>
>
> On Friday 09 September 2011 04:38:44 stat.kk wrote:
> > Hi,
> >
> > can anyone help me how to use 'subset' function on my data frame?
> > I have created data frame 'data' with a few variables and with row names.
> > Now I would like to subset rows with concrete row names.
> > Using data[] I know how to do it. But I dont know how to formulate the
> > subset condition:
> > subset(data, subset = ?, select = c(var1, var2))
> >
> > Thank you very much,
> > stat.kk
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/Subset-function-tp3801397p3801397.html Sent
> > from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > 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.
>
> ______________________________________________
> 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