From ripley at stats.ox.ac.uk  Tue Mar 29 10:24:47 2011
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 29 Mar 2011 09:24:47 +0100 (BST)
Subject: [Rd] two minor bugs in rowsum()
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D70004086C27@NA-PA-VBE03.na.tibco.com>
References: <77EB52C6DD32BA4D87471DCD70C8D70004086C27@NA-PA-VBE03.na.tibco.com>
Message-ID: <alpine.LFD.2.02.1103290921430.17109@gannet.stats.ox.ac.uk>

On Fri, 25 Mar 2011, William Dunlap wrote:

> (a) In R 2.12.2 rowsum can overflow if given an integer input:
>  > rowsum(c(2e9L, 2e9L), c("a", "a"))
>          [,1]
>  a -294967296
>  > 2^32 + .Last.value
>     [,1]
>  a 4e+09
> Should it be changed to coerce its x argument to numeric
> (double precision) so it always returns a numeric output?

No, I don't think so.  But it should return NA on overflow (as sum() 
does), and I've altered pre-2.13.0 to do so.

> (b) When rowsum is given an x containing both NaN and NA it
> appears to use the last of the NaN/NA entries to determine
> if the output is NaN or NA while the `+` function uses the
> first:
>  > z <- cbind( c(NA,NA), c(NA,NaN), c(NaN,NA), c(NaN,NaN))
>  > rowsum(z, c("a","a"))
>    [,1] [,2] [,3] [,4]
>  a   NA  NaN   NA  NaN
>  > z[1,,drop=FALSE] + z[2,,drop=FALSE]
>       [,1] [,2] [,3] [,4]
>  [1,]   NA   NA  NaN  NaN

Which is not a bug: R does not claim to be consistent about this 
(except for a few documented functions), and there are lots of 
instances of this.

> (The name rowsum is a metabug, since it may be confused
> with the entirely different rowSums, but it has been around
> for a long time.)

A lot longer than rowSums ...

> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] two minor bugs in rowsum()
Prof Brian Ripley