I am trying to extract zonal statistics for different classes in R.
I have a raster with two classes (0,1) and need the area (or percentage) of each class under the area of each polygon. I tried exactextractr::exact_extract
but I don't manage to extract a frequency of each class. I can get the sum of class 1, but that wont tell me the proportions.
R – Performing Zonal Statistics for Classes
rrasterzonal statistics
Best Answer
You can get the frequency of a single class by passing a custom summary function to
exact_extract
. For example, to get the fraction of pixels that have a value of1
, you could run:If you have an arbitrary number of classes, here's a solution that will provide the frequencies of each class in each polygon. It does not require knowing the classes in advance, nor does it require loading all intersected pixels into memory.
Basically, we provide a function to
exact_extract
that returns a one-row data frame for each polygon, with a column containing the frequency of each class found in that polygon. Doing this with a callback (i.e., specifying thefun
argument) is important, because otherwise R must store every pixel that intersects every polygon in memory at the same time. With the callback, these pixels are reduced to a frequency table as each polygon is processed. Internally,exact_extract
usesdplyr::bind_rows
to merge the data frames for each polygon, which handles the fact that not all classes are present in each data frame. However, it fills inNA
for the frequency of missing classes, so we use a finalmutate
call to replace these with zero.