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?

1 Answer 1


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

  • 1
    And ?terra is also helpful. freq is in the same group as zonal and summary Commented Feb 5, 2022 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.