[R] replacing NA values in an expression

Ben Bolker ben at zoo.ufl.edu
Sun Apr 16 00:10:54 CEST 2000

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

```