I would like to select rows if a row contains any one of several values. I can do the selection as follows: result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" But this is very unwieldily if one wishes to select many, many rows as one has to continuously repeat the source: result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" | result[,"Subject"]=="John" | result[,"Subject"]=="Mary" Is there an easier way? I tried the following but it did not work: result[,"Subject"]==c("JEFF" | "BG" | "John" | "Mary") Thanks, John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}
On Apr 26, 2010, at 10:12 AM, John Sorkin wrote:> I would like to select rows if a row contains any one of several values. I can do the selection as follows: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" > > But this is very unwieldily if one wishes to select many, many rows as one has to continuously repeat the source: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" | result[,"Subject"]=="John" | result[,"Subject"]=="Mary" > > Is there an easier way? I tried the following but it did not work: > > > result[,"Subject"]==c("JEFF" | "BG" | "John" | "Mary") > > Thanks, > JohnJohn, Try: subset(result, Subject %in% c("JEFF", "BG", "John", "Mary")) See ?subset and ?"%in%" HTH, Marc Schwartz
On Apr 26, 2010, at 11:12 AM, John Sorkin wrote:> I would like to select rows if a row contains any one of several > values. I can do the selection as follows: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" > > But this is very unwieldily if one wishes to select many, many rows > as one has to continuously repeat the source: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" | > result[,"Subject"]=="John" | result[,"Subject"]=="Mary" > > Is there an easier way? I tried the following but it did not work: > > > result[,"Subject"]==c("JEFF" | "BG" | "John" | "Mary")?%in% To select rows the logical vector needs to be in the first position of [ , ] result[result$Subject %in% =c("JEFF" , "BG" , "John" , "Mary") , ] -- David.> > Thanks, > John > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped: > 6}} > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
See ?%in% result$Subject %in% c("JEFF", "BG") John Sorkin wrote:> I would like to select rows if a row contains any one of several values. I can do the selection as follows: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" > > But this is very unwieldily if one wishes to select many, many rows as one has to continuously repeat the source: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" | result[,"Subject"]=="John" | result[,"Subject"]=="Mary" > > Is there an easier way? I tried the following but it did not work: > > > result[,"Subject"]==c("JEFF" | "BG" | "John" | "Mary") > > Thanks, > John > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped:6}} > > ______________________________________________ > 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.
Here are four ways: # state.name comes with R DF <- data.frame(state = state.name, num = seq_along(state.name)) DF[DF$state %in% c("Iowa", "Utah"),] subset(DF, state %in% c("Iowa", "Utah")) subset(DF, grepl("Iowa|Utah", state)) library(sqldf) # see http://sqldf.googlecode.com sqldf("select * from DF where state in ('Iowa', 'Utah')") On Mon, Apr 26, 2010 at 11:12 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:> I would like to select rows if a row contains any one of several values. I can do the selection as follows: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" > > But this is very unwieldily if one wishes to select many, many rows as one has to continuously repeat the source: > > result[,"Subject"]=="JEFF" | result[,"Subject"]=="BG" | result[,"Subject"]=="John" ?| ?result[,"Subject"]=="Mary" > > Is there an easier way? I tried the following but it did not work: > > > result[,"Subject"]==c("JEFF" | "BG" | "John" ?| "Mary") > > Thanks, > John > > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped:6}} > > ______________________________________________ > 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. >