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

rohan sadler rsadler at agric.uwa.edu.au
Mon Apr 28 09:02:06 CEST 2003


Hi all,

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


Cheers in advance

Rohan Sadler

-- 
Ecosystems Research Group (ERGO)
School of Plant Biology (Botany), Faculty of Natural & Agricultural Sciences,
The University of Western Australia, 35 Stirling Highway, Crawley  WA  6009, Australia

Ph:  +61 8 9380 7914
Fax: +61 8 9380 7925
email: rsadler at agric.uwa.edu.au
ERGO's web site:<http://www.botany.uwa.edu.au/ergo>



More information about the R-help mailing list