Cells per category in R terra

rterra

On a categorical SpatRast object, how do I efficiently return a count of cells per factor level? Equivalent to e.g. GRASS r.stats with flag 'c'

summary(x, size = ncell(x)) 

returns a table object with the first few categories, but then lumps the rest into 'other'.

zonal(x, x, length)

works on small example rasters but has memory limits that mean it won't work on large ones. Is there another approach I'm missing?

Best Answer

If you call help(package="terra") you can bring up the help documents and read the function summaries. The function you are looking for is terra::freq