Skip to contents

Methods for the dplyr::rows_insert() family on SpatVector objects.

Usage

# S3 method for class 'SpatVector'
rows_insert(
  x,
  y,
  by = NULL,
  ...,
  conflict = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

# S3 method for class 'SpatVector'
rows_append(x, y, ..., copy = FALSE, in_place = FALSE)

# S3 method for class 'SpatVector'
rows_update(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

# S3 method for class 'SpatVector'
rows_patch(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

# S3 method for class 'SpatVector'
rows_upsert(x, y, by = NULL, ..., copy = FALSE, in_place = FALSE)

# S3 method for class 'SpatVector'
rows_delete(
  x,
  y,
  by = NULL,
  ...,
  unmatched = c("error", "ignore"),
  copy = FALSE,
  in_place = FALSE
)

Arguments

x

A SpatVector.

y

A data frame, sf object or SpatVector.

by

An unnamed character vector giving the key columns. The key columns must exist in both x and y. Keys typically uniquely identify each row, but this is only enforced for the key values of y when rows_update(), rows_patch(), or rows_upsert() are used.

By default, we use the first column in y, since the first column is a reasonable place to put an identifier variable.

...

Other parameters passed onto methods.

conflict

For rows_insert(), how should keys in y that conflict with keys in x be handled? A conflict arises if there is a key in y that already exists in x.

One of:

  • "error", the default, will error if there are any keys in y that conflict with keys in x.

  • "ignore" will ignore rows in y with keys that conflict with keys in x.

copy

If x and y are not from the same data source, and copy is TRUE, then y will be copied into the same src as x. This allows you to join tables across srcs, but it is a potentially expensive operation so you must opt into it.

in_place

Should x be modified in place? This argument is only relevant for mutable backends (e.g. databases, data.tables).

When TRUE, a modified version of x is returned invisibly; when FALSE, a new object representing the resulting changes is returned.

unmatched

For rows_update(), rows_patch(), and rows_delete(), how should keys in y that are unmatched by the keys in x be handled?

One of:

  • "error", the default, will error if there are any keys in y that are unmatched by the keys in x.

  • "ignore" will ignore rows in y with keys that are unmatched by the keys in x.

Value

A SpatVector.

Methods

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

SpatVector

Row operations update attributes while preserving the geometry column. When inserting data frame rows without geometry, the output contains empty geometries for the new rows.

Examples

v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))

rows_update(
  v,
  tibble::tibble(cpro = "05", name = "New name"),
  by = "cpro"
)
#> class       : SpatVector
#> geometry    : polygons
#> dimensions  : 9, 3  (geometries, attributes)
#> extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names       :  iso2  cpro     name
#> type        : <chr> <chr>    <chr>
#> values      : ES-AV    05 New name
#>               ES-BU    09   Burgos
#>               ES-LE    24     Leon
#>               ...

rows_insert(
  v,
  tibble::tibble(cpro = "99", name = "New province"),
  by = "cpro"
)
#> class       : SpatVector
#> geometry    : polygons
#> dimensions  : 10, 3  (geometries, attributes)
#> extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names       :  iso2  cpro   name
#> type        : <chr> <chr>  <chr>
#> values      : ES-AV    05  Avila
#>               ES-BU    09 Burgos
#>               ES-LE    24   Leon
#>               ...