[R] ifelse versus ...
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Apr 29 08:17:09 CEST 2002
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(...,...,...).
>
> 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. ifelse is only more efficient if used to replace constructs like
for(i in seq(along=x)) if(x[i]) foo(x[i]) else bar(x[i])
by ifelse(x, foo(x), bar(x)).
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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