[R] odd feature

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 22 15:57:30 CEST 2006


Due to lazy evaluation, I don't think a and b are fully evaluated:

> ifelse(1, a <- 1, b <- 2)
[1] 1
> a
[1] 1
> b
Error: object "b" not found


On 5/22/06, Martin Maechler <maechler at stat.math.ethz.ch> wrote:
> >>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at gmail.com>
> >>>>>     on Mon, 22 May 2006 09:31:14 -0400 writes:
>
>    Gabor> If you don't like f(if (temp) a else b)
>    Gabor> then what about
>
>    Gabor> temp <- if (test) a else b
>    Gabor> f(temp)
>
>    Gabor> or
>
>    Gabor> temp <- if (test)
>    Gabor> a
>    Gabor> else
>    Gabor> b
>    Gabor> f(temp)
>
>    Gabor> I think its easier to understand if you factor the temp<- out since
>    Gabor> one immediately then knows the purpose of the statement is
>    Gabor> to set temp.
>
> I strongly agree with Gabor on the above.
>
> But, to Duncan's question:
> Yes, indeed, my main point was that people use
> ifelse(test, a, b)  also in cases where test is known to be of
> length one.
>
> BTW, the 2nd point about why I don't ``like'' ifelse() so much
> is on its help page:
>
> Both 'a' and 'b' are fully evaluated even though only one of the
> two values of a[i], b[i] are used in the result.
>
> Martin
>
>    Gabor> On 5/22/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>    >> On 5/22/2006 3:26 AM, Martin Maechler wrote:
>    >> >>>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at 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(.,.,.)
>    >>
>    >> For overuse of ifelse(), do you mean cases where test is length 1, so
>    >> if() would work?  Or are you thinking of something else?
>    >>
>    >> I'd also be interested in what you mean by "quite suboptimal" code.  Are
>    >> you thinking of things like
>    >>
>    >> if (test)
>    >> temp <- a
>    >> else
>    >> temp <- b
>    >> result <- f(temp)
>    >>
>    >> versus
>    >>
>    >> result <- f( if (test) a else b )
>    >>
>    >> ?
>    >>
>    >> I would generally use the former, because it's easier to get the
>    >> formatting right, and I find it easier to read.  It's suboptimal in
>    >> speed and memory use because of creating the temp variable, but in most
>    >> cases I think that would be such a small difference that the small
>    >> increase in readability is worthwhile.
>    >>
>    >> Duncan Murdoch
>    >>
>    >> >
>    >> > 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 at 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 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
>    >> >     >>
>    >> >
>    >> >     Gabor> ______________________________________________
>    >> >     Gabor> R-help at 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 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
>    >>
>    >> ______________________________________________
>    >> 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
>    >>
>



More information about the R-help mailing list