RenE J.V. Bertin wrote:> I find myself doing lots of tests like > > >>subset( data, symptoms=='a' | symptoms=='e' | symptoms=='z' .... ) > > > with symptoms one of the factors contained in the data frame. > > and I wonder if there is not an existing operator or function which implements this sort of repeated conditional in a more space-efficient fashion, something like > > >>subset( data, symptoms==c('a','e','z') )You are looking for %in%: subset(data, symptoms %in% c('a','e','z')) Uwe Ligges> > > (which is incorrect unless symptoms is, in this case, an integer multiple of 3 long). > > Is there, or if not, is there a more efficient/elegant way than > > select.elements <- function(data, lst) > { > slctn <- data == lst[1] > for( e in 2:length(lst) ){ > slctn <- slctn | (data==lst[e]) > } > slctn > } > > > ##> select.elements( 1:10, c(1,5,10) ) > [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE > > Thanks, > RenE > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Chuck Cleland
2005-Jul-08 16:24 UTC
[R] how to do something like symptoms==c('a', 'e', 'z')
RenE J.V. Bertin wrote:> I find myself doing lots of tests like > > >>subset( data, symptoms=='a' | symptoms=='e' | symptoms=='z' .... ) > > > with symptoms one of the factors contained in the data frame. > > and I wonder if there is not an existing operator or function which implements this sort of repeated conditional in a more space-efficient fashion, something likeHow about something like this: mydata <- data.frame(Y = runif(100), X = rep(letters[1:5], 20)) subset(mydata, X %in% c("a", "c", "d"))>>subset( data, symptoms==c('a','e','z') )subset(data, symptoms %in% c("a", "e", "z"))> > (which is incorrect unless symptoms is, in this case, an integer multiple of 3 long). > > Is there, or if not, is there a more efficient/elegant way than > > select.elements <- function(data, lst) > { > slctn <- data == lst[1] > for( e in 2:length(lst) ){ > slctn <- slctn | (data==lst[e]) > } > slctn > } > > > ##> select.elements( 1:10, c(1,5,10) ) > [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE > > Thanks, > RenE > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894