Compute zonal statistics, that is summarized values of a SpatRaster for each "zone" defined by another SpatRaster.
If stat is a true function, zonal will fail (gracefully) for very large Raster objects, but it will in most cases work for functions that can be defined as by a character argument ('mean', 'sd', 'min', 'max', or 'sum'). In addition you can use 'count' to count the number of cells in each zone (only useful with na.rm=TRUE, otherwise freq(z) would be more direct.
If a function is used, it should accept a na.rm argument (or at least a ... argument)