Search code examples
rraster

Why Fun in Zonal gives the same results for "min","mean","count"?


I have two rasters that I would like to compute a 'zonal mean' and number of samples used to compute the mean.It worked fine with the mean but when I wanted to compute the number of samples ''count'', it gave the same results with 'mean' .I tried also with '"min"' which gave the same.

        Library (raster)
        Library (rasterVis)
      Library (lattic)
    r <- raster(nrows=10, ncols=10)
    r <- setValues(r, 1:ncell(r))
    r1 <- raster(nrows=10, ncols=10)
    r1 <- setValues(r1, 1:ncell(r))
    St=stack(r,r1)
    idx <- seq(as.Date('2008-01-15'), as.Date('2008-1-16'), 'day')
     SISmm <- setZ(St, idx)
   dirLayer <- init(SISmm, v='y')
 z <- zonal(SISmm, dirLayer, FUN='mean', digits=2) ## worked fine
 zc <- zonal(SISmm, dirLayer, FUN='count',na.rm=T, digits=2)## the results are the same as z
 zsd <- zonal(SISmm, dirLayer, FUN='sd',na.rm=T, digits=2)## the results are the same as z

Any help please?


Solution

  • Primary error is that the argument name is fun, not FUN . (in previous versions of the package the argument was stat . So much for back-compatibility)

    You will have problems with count, which is deprecated in favor of freq, which requires a raster-ish object, but so far as i can tell zonal converts the data to numeric before passing it to the called function. Possibly you could do zonal(SISmm, dirLayer, stat=function(k) freq(as.raster(k)) but I haven't tried that out.