[R] colSums in C

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Dec 18 23:53:50 CET 2001

David Brahm  <brahm at alum.mit.edu> writes:

> I wrote:
> > I asked how to write speedy C code to implement colSums()...
> > I've taken Doug [Bates]'s code, added names to the result, and included an
> > na.rm flag.  Unfortunately, my na.rm option makes it really slow again
> > ... no faster than pre-processing the matrix with "m[is.na(m)] <- 0".
> Well, I'm an idiot.  I had forgotten to remove the R code that does that
> pre-processing, so no wonder it wasn't any faster!  After fixing that, I got
> results closer to those reported by Thomas Lumley, about 2.18s with ISNA, and
> even faster (1.41s) with isnan (Peter Dalgaard's suggestion).  Is there any
> reason to distrust isnan()?

It's not logically doing the same thing. NA is an NaN, but you can
also get NaN from 0/0 like construction, and you way want the sum to
be NaN in that case. By definition, an NA is a NaN with a special code
(1954, as I recall it) in an otherwise unspecified area. 

There is also a portability issue in the availability of isnan(). R
jumps through a few hoops to ensure that R_IsNA does sensible things
on as many systems as possible, although we may be running out of IEEE
non-compatible systems these days.

It is probably possible to do per-architecture optimizing and inlining
of R_IsNA, and that might be worth looking into.

   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