[R] Selecting cases from a data frame
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Tue Mar 19 21:44:18 CET 2002
On Tue, 19 Mar 2002, Andrew Perrin wrote:
> My apologies if this is obvious - I assume it is but still can't seem to
> make it work.
> I've got a data frame acps.df; I want to run an analysis on a subset of
> that data frame, defined as the subset for which the value of OGMCAT (a
> column in the data frame) is anything OTHER than 9, 11, or 12. So I'd
> like to define a new data frame that consists of the same variables as in
> acps.df, but without those cases. I tried this:
> > acps.nopg.df<-acps.df[(acps.df$OGMCAT != 9) & (acps.df$OGMCAT != 11) &
> (acps.df$OGMCAT != 12)]
> but got this:
> Error in "[.data.frame"(acps.df, (acps.df$OGMCAT != 9) & (acps.df$OGMCAT
> != :
> undefined columns selected
acps.nopg.df<-acps.df[!(acps.df$OGMCAT) %in% c(9,10,12)), ]
You omitted a critical comma: 1D indexing of data frames is by columns.
Using match (via %in% or is.element) is more elegant and clearer.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help