[R] inefficient ifelse() ?
ivo welch
ivowel at gmail.com
Tue Mar 1 20:19:26 CET 2011
dear R experts---
t <- 1:30
f <- function(t) { cat("f for", t, "\n"); return(2*t) }
g <- function(t) { cat("g for", t, "\n"); return(3*t) }
s <- ifelse( t%%2==0, g(t), f(t))
shows that the ifelse function actually evaluates both f() and g() for
all values first, and presumably then just picks left or right results
based on t%%2. uggh... wouldn't it make more sense to evaluate only
the relevant parts of each vector and then reassemble them?
/iaw
----
Ivo Welch
More information about the R-help
mailing list