[R] ifelse versus ...

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Apr 29 08:36:56 CEST 2002

Francisco J Molina wrote:
> I read somewhere that, instead of the structure if(...) else(...)
> it is better to use the command ifelse(...,...,...).
> Currently I am using structures like
> ifelse(...,{sequence; of; commands}, 0)
> instead of if(...){sequence; of; commands}
> and I am using
> ifelse(...,{sequence; of; commands}, {another; Sequence; Of; Commands })
> instead of
> if(...){sequence; of; commands}
> else{another; Sequence; Of; Commands })
> In this way the program becomes a little bit more difficult to read but
> I think that I am optimizing the performance.
> The question is:
> Am I actually optimizing the performance?

No. You can figure it out yourself by a little "simulation" (looping
over if() else() and ifelse()).
ifelse() is designed (and more efficient) for vectorized procedures.

Uwe Ligges
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list