Learn R Programming

tidyterra (version 0.7.2)

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

Description

Bind any number of SpatVector, data frames and sf/sfc objects by row, making a longer result. This is similar to do.call(rbind, dfs), 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 should be a SpatVector and each of the subsequent arguments can either be a SpatVector, a sf/sfc object or a data frame. Columns are matched by name, and any missing columns will be 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 element of ... should be a SpatVector. Subsequent elements may be SpatVector, sf/sfc objects or data frames:

  • If subsequent SpatVector/sf/sfc objects present a different CRS than the first element, those elements would be reprojected to the CRS of the first element with a message.

  • If any element of ... is a tibble/data frame the rows would be cbinded with empty geometries with a message.

See Also

dplyr::bind_rows()

Other dplyr verbs that operate on pairs Spat*/data.frame: bind_cols.SpatVector, filter-joins.SpatVector, mutate-joins.SpatVector

Other dplyr methods: arrange.SpatVector(), bind_cols.SpatVector, count.SpatVector(), distinct.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

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 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
sfobj <- sf::st_as_sf(v2[1, ])

sfobj

bind_spat_rows(v1, sfobj)

# Would reproject with a message on different CRS
sfobj_3857 <- as_spatvector(sfobj) %>% project("EPSG:3857")

bind_spat_rows(v1, sfobj_3857)

# And with 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, ]))

# Or named list combined 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