[R] Selecting cases from a data frame

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Mar 19 21:51:06 CET 2002


Andrew Perrin <andrew_perrin at unc.edu> writes:

> 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
> 
> Thanks for any advice.

Looks like you need a comma in the indexing there. Indexing a data
frame by a single index means that you are selecting columns, so you
need two, the second one of which is empty.

BTW, this stuff is easier written as:

acps.nopg.df<-acps.df[acps.df$OGMCAT %in% c(9,11,12),]


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 mailing list