View source: R/count-tally-SpatVector.R
| count.SpatVector | R Documentation |
SpatVector groupcount() lets you quickly count the unique values of one or more variables:
df |> count(a, b) is roughly equivalent to
df |> group_by(a, b) |> summarise(n = n()). count() is paired with
tally(), a lower-level helper that is equivalent to
df |> summarise(n = n()). Supply wt to perform weighted counts,
switching the summary from n = n() to n = sum(wt).
add_count() is equivalent to count() but use mutate() instead of
summarise() so that it adds a new column with group-wise counts.
## S3 method for class 'SpatVector'
count(
x,
...,
wt = NULL,
sort = FALSE,
name = NULL,
.drop = deprecated(),
.dissolve = TRUE
)
## S3 method for class 'SpatVector'
tally(x, wt = NULL, sort = FALSE, name = NULL)
## S3 method for class 'SpatVector'
add_count(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = deprecated())
A SpatVector object with an additional attribute.
terra::aggregate()
Implementation of the generic dplyr::count() methods for SpatVector
objects.
tally() will always return a disaggregated geometry while count() can
handle this. See also summarise.SpatVector().
dplyr::count(), dplyr::tally()
Other dplyr verbs that operate on group of rows:
group-by.SpatVector,
rowwise.SpatVector(),
summarise.SpatVector()
Other dplyr methods:
arrange.SpatVector(),
bind_cols.SpatVector,
bind_rows.SpatVector,
distinct.SpatVector(),
filter-joins.SpatVector,
filter.Spat,
glimpse.Spat,
group-by.SpatVector,
mutate-joins.SpatVector,
mutate.Spat,
pull.Spat,
relocate.Spat,
rename.Spat,
rowwise.SpatVector(),
select.Spat,
slice.Spat,
summarise.SpatVector()
library(terra)
f <- system.file("ex/lux.shp", package = "terra")
p <- vect(f)
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.