Learn R Programming

tidyterra (version 1.3.0)

nest_join.SpatVector: Nest join SpatVector objects

Description

[Experimental]

nest_join() returns a tibble with the attributes and geometry of x, plus a list-column containing matching rows from y.

Usage

# S3 method for SpatVector
nest_join(
  x,
  y,
  by = NULL,
  copy = FALSE,
  keep = NULL,
  name = NULL,
  ...,
  na_matches = c("na", "never")
)

Value

A tibble.

Arguments

x

A SpatVector.

y

A data frame. Spatial y inputs are not supported, use spatial joins from terra for that workflow.

by

A join specification created with join_by(), or a character vector of variables to join by.

If NULL, the default, *_join() will perform a natural join, using all variables in common across x and y. A message lists the variables so that you can check they're correct; suppress the message by supplying by explicitly.

To join on different variables between x and y, use a join_by() specification. For example, join_by(a == b) will match x$a to y$b.

To join by multiple variables, use a join_by() specification with multiple expressions. For example, join_by(a == b, c == d) will match x$a to y$b and x$c to y$d. If the column names are the same between x and y, you can shorten this by listing only the variable names, like join_by(a, c).

join_by() can also be used to perform inequality, rolling, and overlap joins. See the documentation at ?join_by for details on these types of joins.

For simple equality joins, you can alternatively specify a character vector of variable names to join by. For example, by = c("a", "b") joins x$a to y$a and x$b to y$b. If variable names differ between x and y, use a named character vector like by = c("x_a" = "y_a", "x_b" = "y_b").

To perform a cross-join, generating all combinations of x and y, see cross_join().

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.

keep

Should the new list-column contain join keys? The default will preserve the join keys for inequality joins.

name

The name of the list-column created by the join. If NULL, the default, the name of y is used.

...

Other parameters passed onto methods.

na_matches

Should two NA or two NaN values match?

  • "na", the default, treats two NA or two NaN values as equal, like %in%, match(), and merge().

  • "never" treats two NA or two NaN values as different, and will never match them together or to any other values. This is similar to joins for database sources and to base::merge(incomparables = NA).

Methods

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

The output is a tibble with the attributes and WKT geometry of x, plus a list-column with matching rows from y.

See Also

dplyr::nest_join().

Other dplyr verbs that operate on pairs of SpatVector and data frame objects: bind_cols.SpatVector, bind_rows.SpatVector, cross_join.SpatVector(), filter-joins.SpatVector, mutate-joins.SpatVector, rows.SpatVector

Examples

Run this code
v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
extra <- tibble::tibble(cpro = c("05", "09"), value = c(1, 2))

nest_join(v, extra, by = "cpro")

Run the code above in your browser using DataLab