[R] how to do something like symptoms==c('a', 'e', 'z')
Chuck Cleland
ccleland at optonline.net
Fri Jul 8 18:24:39 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
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 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
More information about the R-help
mailing list