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>
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
Okay, This is one of the cases where I can answer my own foolishness. If I don't define the switch as na.rm=TRUE, but instead just as TRUE - which is my usual practice with other functions - then sum counts the TRUE as another vector object, and counts it as one. So much for saving keystrokes. rohan sadler wrote:> 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 >-- PhD Student, 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>