Learn R Programming

tidyterra (version 1.3.0)

bind_cols.SpatVector: Bind multiple SpatVector, sf and data frame objects by column

Description

Bind any number of SpatVector, data frames and sf objects by column, making a wider result. This is similar to do.call(cbind, data_frames).

Where possible, prefer using a join to combine SpatVector and data frame objects. bind_spat_cols() binds rows in the order in which they appear, so it is easy to create meaningless results without realizing it.

Usage

bind_spat_cols(
  ...,
  .name_repair = c("unique", "universal", "check_unique", "minimal")
)

Value

A SpatVector with the corresponding columns. The geometry and CRS correspond to the first SpatVector in ....

Arguments

...

Objects to combine. The first argument must be a SpatVector. Each subsequent argument can be a SpatVector, sf object or data frame. Inputs are recycled to the same length, then matched by position.

.name_repair

One of "unique", "universal", or "check_unique". See vctrs::vec_as_names() for the meaning of these options.

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

cbind() method.

Methods

Implementation of the dplyr::bind_cols() function for SpatVector objects. For the second and subsequent arguments in ..., the geometry is not cbinded and only the data frame-like columns are kept.

See Also

dplyr::bind_cols().

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

Examples

Run this code
library(terra)
sv <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
df2 <- data.frame(letters = letters[seq_len(nrow(sv))])

# Data frame
bind_spat_cols(sv, df2)

# Another SpatVector
bind_spat_cols(sv[1:2, ], sv[3:4, ])

# sf objects
sfobj <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))

bind_spat_cols(sv[1:9, ], sfobj[1:9, ])

# Mixed

end <- bind_spat_cols(sv, sfobj[seq_len(nrow(sv)), 1:2], df2)

end
glimpse(end)

# Row sizes must be compatible when column-binding.
try(bind_spat_cols(sv, sfobj))

Run the code above in your browser using DataLab