Search code examples
rpolygonr-rasterterra

Using exactextractr to get kurtosis and skewness


I am using the exactexactr package to summarize raster data by polygon. I would like to get skewness, kurtosis, and IQR, but these functions are not built-in to the package. Is there a way to get these stats from this package? My code looks like this, but it is nowhere near working, it seems. How do I write the kurtosis or skewness function inside the exact_extract to get it to work as does mean, max, min, etc?

Edit: skewness is a function inside the e1091 package.

kurt_data <- exact_extract(gridmet_etr, grid_1000, function(value){kurtosis(value)}, progress = FALSE,summarize_df = TRUE)

Solution

  • With terra::extract

    library(terra)
    v <- vect(system.file("ex/lux.shp", package="terra"))
    r <- rast(system.file("ex/elev.tif", package="terra"))
    
    e1 <- extract(r, v, moments::kurtosis, na.rm=TRUE)
    e2 <- extract(r, v, moments::skewness, na.rm=TRUE)
    

    And with exact_extractr you can do things like this:

    k <- exactextractr::exact_extract(r, sf::st_as_sf(v),
            moments:::kurtosis, summarize_df = TRUE, na.rm=T)
    

    But I am not sure if it makes sense to consider partly covered cells in this case (as you are not using a function that accepts weights)