If built-in operations are insufficient, you can pass an arbitrary R function to the fun parameter. Because exactextractr accounts for partial cell coverage, your custom function must accept two arguments: values (the raster values) and coverage_fraction (the fraction of the cell area covered by the polygon).
If you also provide a weights argument, your custom function must accept a third argument: weights.
Examples:
# Number of cells covered by the polygon (ignoring raster values)
exact_extract(rast, poly, function(values, coverage_fraction) sum(coverage_fraction))
# Sum of values weighted by coverage fraction
exact_extract(rast, poly, function(values, coverage_fraction) sum(values * coverage_fraction, na.rm=TRUE))
# Number of distinct values in cells more than 10% covered
exact_extract(rast, poly, function(values, coverage_fraction) length(unique(values[coverage_fraction > 0.1])))
# Number of cells covered by the polygon (raster values are ignored)
exact_extract(rast, poly, function(values, coverage_fraction)
sum(coverage_fraction))
# Sum of defined raster values within the polygon, accounting for coverage fraction
exact_extract(rast, poly, function(values, coverage_fraction)
sum(values * coverage_fraction, na.rm=TRUE))
# Number of distinct raster values within the polygon (coverage fractions are ignored)
exact_extract(rast, poly, function(values, coverage_fraction)
length(unique(values)))
# Number of distinct raster values in cells more than 10% covered by the polygon
exact_extract(rast, poly, function(values, coverage_fraction)
length(unique(values[coverage_fraction > 0.1])))