[Rd] computation with vectors of length 0 (PR#2716)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 1 17:13:43 MEST 2003

Your report is `obviously misleading', as you seem unaware of the standard 
mathematical definitions.  You also seem unaware that R statements are
terminated by a newline, so you have a while series of empty statements
and should not have the semicolons.

The sum of an empty vector is 0, by definition.
The mean of an empty vector is 0/0 = NaN, by definition.
The variance and hence the standard deviation are undefined unless n >= 1.

None of them are missing, so it would be incorrect to return NA.

On Tue, 1 Apr 2003 klaus.hermann at metagen.de wrote:

> Full_Name: Klaus Hermann
> Version: 1.5.0
> OS: SUNRAY - Unix
> Submission from: (NULL) (
> if we produce a numeric vector of length 0 and want compute the sum over its
> elements
> the sum function returns 0 - this is obviously misleading. Better would be to
> return
> NA  and give a corresponding warning.
> There should be a unified strategy to handle vectors of length 0.

There is: to use the standard mathematical definitions.  One could debate 
what var(numeric(0)) should be, but we decided that was most consistently 
an error.

> For example the functions sum, mean, sd handle such vectors in a different way
> example:
> classfreq <- c(3,3,3,3);
> classfreq <- classfreq[classfreq != 3];
> sum(classfreq);
> mean(classfreq);
> sd(classfreq);

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

More information about the R-devel mailing list