Search code examples
rparallel-processingextract

How to pass multiple function in raster::extract()


I'm working with a raster CHM and I've to extract several metrics from a polygon shapefile. Now I'm doing something like this:

library(raster)
library(sp)

#from the help page of extract
r <- raster(ncol=36, nrow=18, vals=1:(18*36))
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- spPolygons(cds1, cds2)

#metrics extraction
mean <- extract(r, polys,mean,df=T)
min<-extract(r, polys,min,df=T)
max<-extract(r, polys,max,df=T)
#and so on for other summary functions (like sd, mode, median, sum etc...)

I would know if there is a way to pass all the summary functions to the fun= argument of the extract() function and if it's possible to do it in parallel. Thanks for every help.

N.B. this is my first question in StackOverflow, I apologize for any impropriety


Solution

  • As @dww suggests above in the comments, here is a function which calculates a number of summary statistics and returns them as a vector. It is passed to the fun argument of raster::extract. Note that the documentation from raster::extract says that the function must accept a na.rm argument. I was unable to change the default behavior of extract for naming the columns of the data frame output so I manually set the names afterward.

    Code

    my_summary <- function(x, na.rm) c(mean = mean(x, na.rm=na.rm), min = min(x, na.rm=na.rm), max = max(x, na.rm=na.rm))
    r_summary <- extract(r, polys, fun = my_summary, df = TRUE)
    names(r_summary) <- c('ID', 'mean', 'min', 'max')
    

    Output

      ID     mean min max
    1  1 387.8158 326 507
    2  2 321.0800 172 498