# \donttest{
library(terra)
f <- system.file("ex/lux.shp", package = "terra")
p <- vect(f)
p |> count(NAME_1, sort = TRUE)
p |> count(NAME_1, sort = TRUE)
p |> count(pop = ifelse(POP < 20000, "A", "B"))
# tally() is a lower-level function that assumes you've done the grouping
p |> tally()
p |>
group_by(NAME_1) |>
tally()
# Dissolve geometries by default
library(ggplot2)
p |>
count(NAME_1) |>
ggplot() +
geom_spatvector(aes(fill = n))
# Opt out
p |>
count(NAME_1, .dissolve = FALSE, sort = TRUE) |>
ggplot() +
geom_spatvector(aes(fill = n))
# }
Run the code above in your browser using DataLab