Learn R Programming

tidyterra (version 1.3.0)

bind_rows.SpatVector: Bind multiple SpatVector, sf, sfc and data frame objects by row

Description

Bind any number of SpatVector, data frames, sf and sfc objects by row, making a longer result. This is similar to do.call(rbind, data_frames), but the output will contain all columns that appear in any of the inputs.

Usage

bind_spat_rows(..., .id = NULL)

Value

A SpatVector of the same type as the first element of ....

Arguments

...

Objects to combine. The first argument must be a SpatVector. Each subsequent argument can be a SpatVector, sf or sfc object or a data frame. Columns are matched by name and any missing columns are filled with NA.

.id

The name of an optional identifier column. Provide a string to create an output column that identifies each input. The column will use names if available, otherwise it will use positions.

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

rbind() method.

Methods

Implementation of the dplyr::bind_rows() function for SpatVector objects.

The first argument should be a SpatVector. Each subsequent argument can be a SpatVector, an sf or sfc object or a data frame:

  • If subsequent spatial objects have a different CRS from the first element, they are reprojected to the CRS of the first element with a message.

  • If any element of ... is a tibble/data frame, the rows are column-bound with empty geometries with a message.

See Also

dplyr::bind_rows().

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

Examples

Run this code

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

v1 <- v[1, "cpro"]
v2 <- v[3:5, c("name", "iso2")]

# You can supply individual SpatVector objects as arguments.
bind_spat_rows(v1, v2)

# When you supply a column name with the `.id` argument, a new column is
# created to link each row to its original data frame.
bind_spat_rows(v1, v2, .id = "id")

# \donttest{
# Use with sf objects.
sfobj <- sf::st_as_sf(v2[1, ])

sfobj

bind_spat_rows(v1, sfobj)

# Reproject with a message when the CRS differs.
sfobj_3857 <- as_spatvector(sfobj) |> project("EPSG:3857")

bind_spat_rows(v1, sfobj_3857)

# Bind data frames with a message.
data("mtcars")
bind_spat_rows(v1, sfobj, mtcars, .id = "id2")

# Use lists
bind_spat_rows(list(v1[1, ], sfobj[1:2, ]))

# Use a named list with `.id`.
bind_spat_rows(list(
  SpatVector = v1[1, ], sf = sfobj[1, ],
  mtcars = mtcars[1, ]
), .id = "source")
# }

Run the code above in your browser using DataLab