[R] 0 * NA = NA

Liaw, Andy andy_liaw at merck.com
Mon Mar 5 16:02:33 CET 2007


From: Alberto Monteiro
> 
> Is there any way to "force" 0 * NA to be 0 instead of NA?
> 
> For example, suppose I have a vector with some valid values, 
> while other values are NA. If I matrix-pre-multiply this by a 
> weight row vector, whose weights that correspond to the NAs 
> are zero, the outcome will still be NA:
> 
> x <- c(1, NA, 1)
> wt <- c(2, 0, 1)
> wt %*% x # NA

I don't think it's prudent to bend arthmetic rules of a system,
especially when there are good reasons for them.  Here's one:

R> 0 * Inf
[1] NaN

If you are absolutely sure that the Nas in x cannot be Inf (or -Inf),
you might try to force the result to 0, but the only way I can think of
is to do something like:

R> wt %*% ifelse(wt, x, 0)
     [,1]
[1,]    3

Andy 

 
> Alberto Monteiro
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}



More information about the R-help mailing list