
Keep distinct/unique rows and geometries of SpatVector objects
Source: R/distinct-SpatVector.R
distinct.SpatVector.RdKeep only unique/distinct rows and geometries from a SpatVector.
Usage
# S3 method for class 'SpatVector'
distinct(.data, ..., .keep_all = FALSE)Arguments
- .data
A
SpatVectorcreated withterra::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, will use all variables in the data frame. There is a reserved variable name,geometry, that would remove 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.
terra equivalent
Methods
Implementation of the generic dplyr::distinct() function.
See also
dplyr::distinct(), terra::unique()
Other dplyr verbs that operate on rows:
arrange.SpatVector(),
filter.Spat,
slice.Spat
Other dplyr methods:
arrange.SpatVector(),
bind_cols.SpatVector,
bind_rows.SpatVector,
count.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
library(terra)
v <- vect(system.file("ex/lux.shp", package = "terra"))
# Create a vector with dups
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
#> class : SpatVector
#> geometry : polygons
#> dimensions : 34, 7 (geometries, attributes)
#> extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : ID_1 NAME_1 ID_2 NAME_2 AREA POP gr
#> type : <num> <chr> <num> <chr> <num> <num> <chr>
#> values : 1 Diekirch 1 Clervaux 312 1.808e+04 B
#> 3 Luxembourg 11 Mersch 233 3.211e+04 A
#> 1 Diekirch 3 Redange 259 1.866e+04 B
nrow(ex1)
#> [1] 34
# Duplicates by NAME_1
ex2 <- distinct(v, gr)
ex2
#> class : SpatVector
#> geometry : polygons
#> dimensions : 3, 1 (geometries, attributes)
#> extent : 5.826232, 6.312236, 49.51847, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : gr
#> type : <chr>
#> values : B
#> A
#> C
nrow(ex2)
#> [1] 3
# Same but keeping all cols
ex2b <- distinct(v, gr, .keep_all = TRUE)
ex2b
#> class : SpatVector
#> geometry : polygons
#> dimensions : 3, 7 (geometries, attributes)
#> extent : 5.826232, 6.312236, 49.51847, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : gr ID_1 NAME_1 ID_2 NAME_2 AREA POP
#> type : <chr> <num> <chr> <num> <chr> <num> <num>
#> values : B 1 Diekirch 1 Clervaux 312 1.808e+04
#> A 3 Luxembourg 11 Mersch 233 3.211e+04
#> C 3 Luxembourg 10 Luxembourg 237 1.826e+05
nrow(ex2b)
#> [1] 3
# Unique geometries
ex3 <- distinct(v, geometry)
ex3
#> class : SpatVector
#> geometry : polygons
#> dimensions : 12, 0 (geometries, attributes)
#> extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
nrow(ex3)
#> [1] 12
# Same as terra::unique()
terra::unique(ex3)
#> class : SpatVector
#> geometry : polygons
#> dimensions : 12, 0 (geometries, attributes)
#> extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
# Unique keeping info
distinct(v, geometry, .keep_all = TRUE)
#> class : SpatVector
#> geometry : polygons
#> dimensions : 12, 7 (geometries, attributes)
#> extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names : ID_1 NAME_1 ID_2 NAME_2 AREA POP gr
#> type : <num> <chr> <num> <chr> <num> <num> <chr>
#> values : 1 Diekirch 1 Clervaux 312 1.808e+04 B
#> 3 Luxembourg 11 Mersch 233 3.211e+04 A
#> 1 Diekirch 3 Redange 259 1.866e+04 B