Learn R Programming

tidyterra (version 0.7.0)

count.SpatVector: Count the observations in each SpatVector group

Description

count() 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()).

Usage

# S3 method for SpatVector
count(
  x,
  ...,
  wt = NULL,
  sort = FALSE,
  name = NULL,
  .drop = group_by_drop_default(x),
  .dissolve = TRUE
)

# S3 method for SpatVector tally(x, wt = NULL, sort = FALSE, name = NULL)

Value

A SpatVector object with an additional attribute.

Arguments

x

A SpatVector.

...

<data-masking> Variables to group by.

wt

Not implemented on this method

sort

If TRUE, will show the largest groups at the top.

name

The name of the new column in the output.

If omitted, it will default to n. If there's already a column called n, it will use nn. If there's a column called n and nn, it'll use nnn, and so on, adding ns until it gets a new name.

.drop

Handling of factor levels that don't appear in the data, passed on to group_by().

For count(): if FALSE will include counts for empty groups (i.e. for levels of factors that don't exist in the data).

[Deprecated] For add_count(): deprecated since it can't actually affect the output.

.dissolve

logical. Should borders between aggregated geometries be dissolved?

<a href="https://CRAN.R-project.org/package=terra"><span class="pkg">terra</span></a> equivalent

terra::aggregate()

Methods

Implementation of the generic dplyr::count() family functions for SpatVector objects.

tally() will always return a disaggregated geometry while count() can handle this. See also summarise.SpatVector().

See Also

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()

Examples

Run this code
# \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