[R] sum() with na.rm=TRUE, again

Jonathan Baron baron at cattell.psych.upenn.edu
Thu Apr 25 18:23:27 CEST 2002


It is apparently a good convention to have the some of nothing be
zero.  But the mean of nothing involves dividing by infinity, so
the mean of c(NA,NA,NA) is NaN, which acts like NA for most
purposes.  So I think the trick is to compute the mean and then
multiply by the number of non-missing cases, e.g.,

apply(matrix1,1,mean,na.rm=T)*apply(!is.na(matrix1),1,sum)

but the all() method will work too.  Jon Baron


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