[R] colSums in C

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Dec 18 20:19:32 CET 2001

On Tue, 18 Dec 2001, David Brahm wrote:

> 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()?

Portability.  Not all possible ports of R have it, and R can be built
without it.  But there are macros to cover that.

> Current colSums code is included below, and there's a rowSums too.  I'm still
> prettying it up, but I'd like to open discussion on what to do with it when
> it's ready for prime time:
>   1) I can just use it locally and be perfectly happy,
>   2) I can upload to CRAN (with Doug's permission, of course),
>   3) I believe Doug Bates and Kurt Hornik once discussed modifying apply() to
>      catch these special cases and use the optimized code.  Dangerous?

Yes. You coerce the summands to be numeric: you want at least to handle
the complex case, and it is probably worth handling separately the
integer/logical case too.

colSums is an S-PLUS function (somewhat more general than this one), and
one of a family {cols,rows}{Means,Sums,Vars,Stdev}.  My guess is that
it is worth including at least the Means and Vars version is a future R.

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