Dear R people, This is simple, nay trivial question. I do the following. I just want to know what is the slickest/most elegant method. I have an expression containing the term (y)*log(y) where y can take the value either 0 or 1. If y is 0 then the expression is undefined, but in this case I want to replace the result by 0 in the expression. I don't want to write a separate function for this, which would be the simplest way of handling it, That is, I want this replacement to be evaluated as part of the expression. What is the best way to do this? Faheem. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 Fri, 14 Apr 2000, Faheem Mitha wrote:> Dear R people, > > This is simple, nay trivial question. I do the following. I just want to > know what is the slickest/most elegant method. I have an expression > containing the term > > (y)*log(y) > > where y can take the value either 0 or 1. If y is 0 then the expression is > undefined, but in this case I want to replace the result by 0 in the > expression. I don't want to write a separate function for this, which > would be the simplest way of handling it, That is, I want this replacement > to be evaluated as part of the expression. What is the best way to do > this?See V&R3 p.94 for this very example. E.g. ifelse(y<=0, 0, y*log(y)) I'm not sure what this has to do with NA's: in R> 0*log(0)[1] NaN -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Are you sure you mean y can be *either* 0 or 1? In that case y*log(y) is always either undefined (in which case you want it set to 0) or 0 in any case (because log(1)=0). If you mean that y is *between* 0 and 1 then ifelse(y==0,0,y*log(y)) should do the trick. On Fri, 14 Apr 2000, Faheem Mitha wrote:> Dear R people, > > This is simple, nay trivial question. I do the following. I just want to > know what is the slickest/most elegant method. I have an expression > containing the term > > (y)*log(y) > > where y can take the value either 0 or 1. If y is 0 then the expression is > undefined, but in this case I want to replace the result by 0 in the > expression. I don't want to write a separate function for this, which > would be the simplest way of handling it, That is, I want this replacement > to be evaluated as part of the expression. What is the best way to do > this? > Faheem. > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > >-- Ben Bolker bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker 318 Carr Hall/Box 118525 tel: (352) 392-5697 Gainesville, FL 32611-8525 fax: (352) 392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._