Learn R Programming

tidyterra (version 1.3.0)

distinct.SpatVector: Keep distinct/unique rows and geometries of SpatVector objects

Description

Keep only unique/distinct rows and geometries from a SpatVector.

Usage

# S3 method for SpatVector
distinct(.data, ..., .keep_all = FALSE)

Value

A SpatVector object.

Arguments

.data

A SpatVector created with terra::vect().

...

<data-masking> Optional variables to use when determining uniqueness. If there are multiple rows for a given combination of inputs, only the first row will be preserved. If omitted, all variables in the data frame are used. There is a reserved variable name, geometry, that removes duplicate geometries. See Methods.

.keep_all

If TRUE, keep all variables in .data. If a combination of ... is not distinct, this keeps the first row of values.

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

terra::unique().

Methods

Implementation of the generic dplyr::distinct() method for SpatVector objects.

You can remove duplicate geometries by passing the reserved name geometry to .... See Examples.

See Also

dplyr::distinct(), terra::unique().

Other dplyr verbs that operate on rows: arrange.SpatVector(), filter.Spat, rows.SpatVector, slice.Spat

Examples

Run this code

library(terra)

v <- vect(system.file("ex/lux.shp", package = "terra"))

# Create a vector with duplicates.
v <- v[sample(seq_len(nrow(v)), 100, replace = TRUE), ]
v$gr <- sample(LETTERS[1:3], 100, replace = TRUE)

# All duplicates
ex1 <- distinct(v)
ex1

nrow(ex1)

# Duplicates by NAME_1
ex2 <- distinct(v, gr)
ex2
nrow(ex2)

# Keep all columns.
ex2b <- distinct(v, gr, .keep_all = TRUE)
ex2b
nrow(ex2b)

# Unique geometries
ex3 <- distinct(v, geometry)

ex3
nrow(ex3)
# Same as terra::unique()
terra::unique(ex3)

# Keep information for unique values.
distinct(v, geometry, .keep_all = TRUE)

Run the code above in your browser using DataLab