Re: [R] simplify this instruction

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Thu, 20 Nov 2008 02:38:49 +0100

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"

>
>> On Wed, Nov 19, 2008 at 4:42 PM, CE.KA <ce.kaya75@yahoo.fr> wrote:
>>
>>
>>> Hi R users,
>>>
>>> Is there a way to simplify this instruction:
>>> ifelse(B=="0","A",
>>> ifelse(B=="1","A",
>>> ifelse(B=="2","A",
>>> ifelse(B=="3","A",
>>> ifelse(B=="4","A",
>>> ifelse(B=="5","A",
>>> ifelse(B=="6","A",
>>> ifelse(B=="7","A",
>>> ifelse(B=="8","A",
>>> ifelse(B=="9","A","B"))))))))))
>>>
>>> i am looking for something like this:
>>>
>>> ifelse(B=="(0:9)","A","B")
>>>
>>>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 20 Nov 2008 - 01:40:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Nov 2008 - 04:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive