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

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Jul 8 18:22:09 CEST 2005


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




More information about the R-help mailing list