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. ---------------------------------------------------------------------- Andrew J Perrin - andrew_perrin at unc.edu - http://www.unc.edu/~aperrin Assistant Professor of Sociology, U of North Carolina, Chapel Hill 269 Hamilton Hall, CB#3210, Chapel Hill, NC 27599-3210 USA -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 selectedacps.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._