# Re: [R] simplify this instruction

From: Rolf Turner <r.turner_at_auckland.ac.nz>
Date: Thu, 20 Nov 2008 17:26:08 +1300

So (presumably) what he *really* wanted to say was (something like)

Of course this is sheer pedantry. I'm sure the original poster was really concerned with a numeric variable and used the quote marks around the digits out of confusion over what was required. (Some misguided subconscious notion to the effect that to get character output you need character input.)

cheers,

Rolf Turner

On 20/11/2008, at 5:06 PM, 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 ifelse(cond, consequenceA, consequenceB)
> function operates on a vector and returns a vector of equal length,
> whereas:
>
> if cond consequenceA else consequenceB # only returns a single
> item.
>
>> B <- 0:12
>> ifelse(B %in% c(0:9), 'A', 'B')
> [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B"
>
>> if (B %in% as.character(0:9)) "A" else "B"
> [1] "A"
> Warning message:
> In if (B %in% as.character(0:9)) "A" else "B" :
> the condition has length > 1 and only the first element will be
> used
>
>> if (as.numeric(B) %in% 0:9) "A" else "B"
> [1] "A"
> Warning message:
> In if (as.numeric(B) %in% 0:9) "A" else "B" :
> the condition has length > 1 and only the first element will be
> used
>
>> 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"))))))))))
> [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B"
>>
>
> --
> David Winsemius
> Heritage Labs
>>
>>
>>>
>>>> On Wed, Nov 19, 2008 at 4:42 PM, CE.KA <ce.kaya75_at_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
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

```######################################################################
```
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

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 - 04:29:54 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 - 09:30:25 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.