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

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Sat 09 Jul 2005 - 02:22:09 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
>
>

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



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:29:28 2005

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