Re: [R] odd feature

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon 22 May 2006 - 17:26:32 EST

>>>>> "Gabor" == Gabor Grothendieck <ggrothendieck@gmail.com> >>>>> on Sun, 21 May 2006 09:47:07 -0400 writes:

    Gabor> If you know that test is a scalar

    Gabor> result <- if (test) a else b

    Gabor> will do it.

Yes, indeed!
IMO, ifelse(test, a, b) is much overused where as

      if(test) a else b is much UNDER used.

>From some e-mail postings, and even some documents (even printed books?), I get the impression that too many people think that  ifelse(.,.,.) is to be used as expression / function and   if(.) . else . only for "program flow control". This leads to quite suboptimal code, and I personally use if(.) . else . __as expression__ much more frequently than ifelse(.,.,.)  

Martin Maechler, ETH Zurich.

    Gabor> Here is another approach:

    Gabor> as.vector(test * ts(a) + (!test) * ts(b))

    Gabor> On 5/21/06, ivo welch <ivowel@gmail.com> wrote:
>> Dear R wizards:
>>
>> I just got stung by the ifelse() feature.
>>
>> > a <- 10:15
>> > b <- 20:300
>> > test <- 1
>> > ifelse(test,a,b)
>> [1] 10
>>
>> I had not realized that this was the default behavior---I had expected
>> 10:15. mea culpa. however, I wonder whether it would make sense to
>> replace ifelse with a different semantic, where if test is a single
>> scalar, it means what a stupid user like me would imagine.
>>
>> Aside, I like the flexibility of R, but I am not thrilled by all the
>> recycling rules. I either mean I want a scalar or a vector of
>> equal/appropriate dimension. I never want a recycle of a smaller
>> vector. (I do often use a recycle of a scalar.)
>>
>> regards,
>>
>> /iaw
>>
>> ______________________________________________
>> 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
>>

    Gabor> ______________________________________________
    Gabor> R-help@stat.math.ethz.ch mailing list
    Gabor> https://stat.ethz.ch/mailman/listinfo/r-help     Gabor> 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 Mon May 22 17:31:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 23 May 2006 - 04:10:21 EST.

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