group_data.SpatVector: Grouping metadata for 'SpatVector' objects

View source: R/group-data-SpatVector.R

group_data.SpatVectorR Documentation

Grouping metadata for SpatVector objects

Description

This collection of functions accesses data about grouped SpatVector objects in various ways:

  • group_data() returns a tibble that defines the grouping structure. The columns give the values of the grouping variables. The last column, always called .rows, is a list of integer vectors that gives the location of the rows in each group.

  • group_keys() returns a tibble describing the groups.

  • group_rows() returns a list of integer vectors giving the rows that each group contains.

  • group_indices() returns an integer vector the same length as .data that gives the group that each row belongs to.

  • group_vars() gives names of grouping variables as character vector.

  • groups() gives the names of the grouping variables as a list of symbols.

  • group_size() gives the size of each group.

  • n_groups() gives the total number of groups.

See dplyr::group_data().

Usage

## S3 method for class 'SpatVector'
group_data(.data)

## S3 method for class 'SpatVector'
group_keys(.tbl, ...)

## S3 method for class 'SpatVector'
group_indices(.data, ...)

## S3 method for class 'SpatVector'
group_vars(x)

## S3 method for class 'SpatVector'
groups(x)

## S3 method for class 'SpatVector'
group_size(x)

## S3 method for class 'SpatVector'
n_groups(x)

Arguments

.data, .tbl, x

A SpatVector.

...

Use of ... is now deprecated; please use group_by() first instead.

Value

See the description of the function. The results are usually tibbles, lists or vectors. These functions does not return SpatVector objects.

Examples

library(terra)

v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
v$gr_1 <- rep_len(c("A", "A", "B"), length.out = nrow(v))
v$gr_2 <- rep_len(c("C", "D"), length.out = nrow(v))

# Ungrouped

n_groups(v)

group_vars(v)

group_keys(v)

group_size(v)

groups(v)

group_rows(v)

group_data(v)

group_indices(v)

# Grouped by one var
gv <- group_by(v, gr_1)

n_groups(gv)

group_vars(gv)

group_keys(gv)

group_size(gv)

groups(gv)

group_rows(gv)

group_data(gv)

group_indices(gv)

# Grouped by several vars

gv2 <- group_by(v, gr_1, gr_2)

n_groups(gv2)

group_vars(gv2)

group_keys(gv2)

group_size(gv2)

groups(gv2)

group_rows(gv2)

group_data(gv2)

group_indices(gv2)

dieghernan/tidyterra documentation built on Feb. 20, 2025, 4:18 p.m.