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? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._