Skip to contents

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

Usage

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

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, 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.

Value

A SpatVector object.

terra equivalent

terra::unique()

Methods

Implementation of the generic dplyr::distinct() function.

SpatVector

It is possible to remove duplicate geometries including the geometry variable explicitly in the ... call. See Examples.

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> <int> <chr>
#>  values      :     1     Diekirch     5      Wiltz   263 16735     A
#>                    2 Grevenmacher     6 Echternach   188 18899     C
#>                    3   Luxembourg     8   Capellen   185 48187     A

nrow(ex1)
#> [1] 34

# Duplicates by NAME_1
ex2 <- distinct(v, gr)
ex2
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 3, 1  (geometries, attributes)
#>  extent      : 5.74414, 6.528252, 49.72324, 50.18162  (xmin, xmax, ymin, ymax)
#>  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#>  names       :    gr
#>  type        : <chr>
#>  values      :     A
#>                    C
#>                    B
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.74414, 6.528252, 49.72324, 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> <int>
#>  values      :     A     1     Diekirch     5      Wiltz   263 16735
#>                    C     2 Grevenmacher     6 Echternach   188 18899
#>                    B     1     Diekirch     1   Clervaux   312 18081
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> <int> <chr>
#>  values      :     1     Diekirch     5      Wiltz   263 16735     A
#>                    2 Grevenmacher     6 Echternach   188 18899     C
#>                    3   Luxembourg     8   Capellen   185 48187     A