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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Apr 25 18:11:36 CEST 2002

"Richards, Tom" <richards at upci.pitt.edu> writes:

> but is there a safer way to write a sum() function?  Or, do these zeroes
> serve some purpose that I am missing?
> Thanks in advance...

Mathematically, the sum over an empty set is zero. This serves various
consistency purposes (sum over disjoint union of two index sets, etc.)

Just use something like

mysum <- function(x) if (all(is.na(x))) NA else sum(x,na.rm=T)

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
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