Hello, I would like to select cases using multiple logical operations (e.g. X or Y or Z) without having to repeat the dataframe$variable within the subscript. My working code (with a single logical operator) currently looks like this: dataframe$newvariable[data$oldvariable=="X"]<-"group1" I thought this next line of code might do what I wanted, but it doesn't: dataframe$newvariable[data$oldvariable=="X" | "Y" | "Z"]<-"group1" I'd appreciate any suggestions. I've tried playing around with grep, but can't make it work. Thanks! Mark
Mark Try dataframe$newvariable[data$oldvariable %in% c("X","Y","Z")] <- "group1" HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Na > Sent: Friday, 19 September 2008 12:11 p.m. > To: R-help at stat.math.ethz.ch > Subject: [R] Multiple logical operations in a subscript > > Hello, > > I would like to select cases using multiple logical > operations (e.g. X or Y or Z) without having to repeat the > dataframe$variable within the subscript. My working code > (with a single logical operator) currently looks like this: > > dataframe$newvariable[data$oldvariable=="X"]<-"group1" > > I thought this next line of code might do what I wanted, but > it doesn't: > > dataframe$newvariable[data$oldvariable=="X" | "Y" | "Z"]<-"group1" > > I'd appreciate any suggestions. I've tried playing around > with grep, but can't make it work. > > Thanks! Mark > > ______________________________________________ > 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.
Peter showed you the %in% operator, you may also want to look at the subset, transform, with, and within functions for future use as ways to reduce the need to type the name of an object multiple times. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Mark Na > Sent: Thursday, September 18, 2008 6:11 PM > To: R-help at stat.math.ethz.ch > Subject: [R] Multiple logical operations in a subscript > > Hello, > > I would like to select cases using multiple logical operations (e.g. X > or Y or Z) without having to repeat the dataframe$variable within the > subscript. My working code (with a single logical operator) currently > looks like this: > > dataframe$newvariable[data$oldvariable=="X"]<-"group1" > > I thought this next line of code might do what I wanted, but it > doesn't: > > dataframe$newvariable[data$oldvariable=="X" | "Y" | "Z"]<-"group1" > > I'd appreciate any suggestions. I've tried playing around with grep, > but > can't make it work. > > Thanks! Mark > > ______________________________________________ > 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.