[R] ifelse versus ...
Ott Toomet
siim at obs.ee
Mon Apr 29 09:40:08 CEST 2002
Hi,
On Sun, 28 Apr 2002, Francisco J Molina wrote:
I read somewhere that, instead of the structure if(...) else(...)
it is better to use the command ifelse(...,...,...).
The question is:

Am I actually optimizing the performance?
I am sorry that I cannot answer exactly your question. But:
 you can find the speed of the alternate expressions using syste.time()
 if() + else() and ifelse() are not quite the same. The first form is for
scalar logic, the second for vector logic. I think if you are using scalars
as your choice variables (e.g. block of expressions is conditional to a
scalar logical variable), then if()else() and ifelse() do equally well. If
you are looking for vector logic, i.e. the expressions for every element in
a vector depend on the corresponding element in a logical vector, then
ifelse should be faster, you need a cycle when doing the same with if() and
else().
When there are many conditions, one may get clearer code when writing as is
common in gauss, something like
a < (b == 1)*expression1 + (b == 2)*expression2 + ....
I hope you got an idea.
Ott Toomet
.......................................
rhelp mailing list  Read http://www.ci.tuwien.ac.at/~hornik/R/RFAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: rhelprequest at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the Rhelp
mailing list