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

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 28 12:01:44 CEST 2003


I can't reproduce this: can you give us the random seed used or a 
reproducible example in some other way.  (Preferably some actual data, as 
the random-number generator default has changed since 1.6.1.)

On Mon, 28 Apr 2003, rohan sadler wrote:

> I get two different results when using sum() and the switch na.rm. The 
> result is correct when na.rm=FALSE.
> Linux Redhat 7.3, R version 1.6.1.
> 
> I've had no luck searching the mail archives, so I was hoping somebody 
> could explain/check this one for me. I will need to apply the function 
> to missing data, simple as it is.
> 
> Code:
> 
>  x<-matrix(runif(20,0,5)%/%1,4,5)      # random matrix data set 4x5, 
> values are integers between 0 and 4
> 
> coords<-c(2,3,3,4)     # choose any sub-matrix, with 
> c(ymin,ymax,xmin,xmax) being row and column indices.
> 
>  occupied.counter<-function(coords){sum(x[coords[3]:coords[4],coords[1]:coords[2]])}
> 
>  occupied.counter(coords)
> [1] 7
> 
>  occupied.counter<-function(coords){sum(x[coords[3]:coords[4],coords[1]:coords[2]], 
> na.rm=TRUE)}
> 
>  occupied.counter(coords)
> [1] 8

-- 
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-help mailing list