[R] simplify this instruction

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Nov 20 10:06:20 CET 2008


David Winsemius wrote:
>
> On Nov 19, 2008, at 8:38 PM, Wacek Kusnierczyk wrote:
>
>> Jorge Ivan Velez wrote:
>>>
>>>> B=0:12
>>>> B
>>>>
>>> [1]  0  1  2  3  4  5  6  7  8  9 10 11 12
>>>
>>>> ifelse(B%in%c(0:9),'A','B')
>>>>
>>> [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B"
>>>
>>>
>>
>> given the example, the solution would rather be
>>
>> if (B %in% as.character(0:9)) "A" else "B"
>>
>> or maybe
>>
>> if (as.numeric(B) %in% 0:9) "A" else "B"
>
> Er, ... The solution offered by Velez duplicates the action of the
> nested ifelse expressions offered by the original poster. Your
> alternatives do not. The 

oops, right.  should have checked agains B as a non-singleton vector.

vQ



More information about the R-help mailing list