[R] inefficient ifelse() ?

Thomas Lumley tlumley at uw.edu
Tue Mar 1 22:59:27 CET 2011


On Wed, Mar 2, 2011 at 9:36 AM, ivo welch <ivo.welch at gmail.com> 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.
>

Ivo,

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


      -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-help mailing list