Re: [R] inefficient ifelse() ?

From: Thomas Lumley <>
Date: Wed, 02 Mar 2011 10:59:27 +1300

On Wed, Mar 2, 2011 at 9:36 AM, ivo welch <> wrote:
> thanks, Henrique.  did you mean
>    as.vector(t(mapply(function(x, f)f(x), split(t, ((t %% 2)==0)),
> list(f, g))))   ?
> otherwise, you get a matrix.
> its a good solution, but unfortunately I don't think this can be used
> to redefine ifelse(cond,ift,iff) in a way that is transparent.  the
> ift and iff functions will always be evaluated before the function
> call happens, even with lazy evaluation.  :-(
> I still think that it makes sense to have a smarter vectorized %if% in
> a vectorized language like R.  just my 5 cents.


There is no guarantee in general that f(x[3,5,7]) is the same as f(x)[3,5,7]


Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 01 Mar 2011 - 22:23:10 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 Tue 01 Mar 2011 - 22:30:17 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive