Re: [R] how to do something like symptoms==c('a', 'e', 'z')

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Sat 09 Jul 2005 - 02:24:39 EST

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

   How 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@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

______________________________________________
R-help@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
Received on Sat Jul 09 02:35:43 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:26 EST