[R] replacing NA values in an expression

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sat Apr 15 07:14:29 CEST 2000

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

More information about the R-help mailing list