Skip to contents

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

Where possible prefer using a join to combine SpatVector and data frames objects. bind_spat_cols() binds the rows in 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")
)

Arguments

...

Objects to combine. The first argument should be a SpatVector and each of the subsequent arguments can either be a SpatVector, a sf object or a data frame. Inputs are recycled to the same length, then matched by position.

.name_repair

One of "unique", "universal", or "check_unique". See dplyr::bind_cols() for Details.

Value

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

terra equivalent

cbind() method

Methods

Implementation of the dplyr::bind_rows() function for SpatVector objects. Note that for the second and subsequent arguments on ... the geometry would not be cbinded, and only the data frame (-ish) columns would be kept.

Examples

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)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 4  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  source      : cyl.gpkg
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro   name letters
#>  type        : <chr> <chr>  <chr>   <chr>
#>  values      : ES-AV    05  Avila       a
#>                ES-BU    09 Burgos       b
#>                ES-LE    24   Leon       c


# Another SpatVector
bind_spat_cols(sv[1:2, ], sv[3:4, ])
#> New names:
#>  `iso2` -> `iso2...1`
#>  `cpro` -> `cpro...2`
#>  `name` -> `name...3`
#>  `iso2` -> `iso2...4`
#>  `cpro` -> `cpro...5`
#>  `name` -> `name...6`
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 2, 6  (geometries, attributes)
#>  extent      : 2987054, 3296229, 2017622, 2331004  (xmin, xmax, ymin, ymax)
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       : iso2...1 cpro...2 name...3 iso2...4 cpro...5 name...6
#>  type        :    <chr>    <chr>    <chr>    <chr>    <chr>    <chr>
#>  values      :    ES-AV       05    Avila    ES-LE       24     Leon
#>                   ES-BU       09   Burgos     ES-P       34 Palencia

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

bind_spat_cols(sv[1:9, ], sfobj[1:9, ])
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 17  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro   name  AREA PERIMETER CNTY_ CNTY_ID      NAME  FIPS
#>  type        : <chr> <chr>  <chr> <num>     <num> <num>   <num>     <chr> <chr>
#>  values      : ES-AV    05  Avila 0.114     1.442  1825    1825      Ashe 37009
#>                ES-BU    09 Burgos 0.061     1.231  1827    1827 Alleghany 37005
#>                ES-LE    24   Leon 0.143      1.63  1828    1828     Surry 37171
#>     FIPSNO (and 7 more)
#>      <num>             
#>  3.701e+04             
#>    3.7e+04             
#>  3.717e+04             

# Mixed

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

end
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 6  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  source      : cyl.gpkg
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro   name  AREA PERIMETER letters
#>  type        : <chr> <chr>  <chr> <num>     <num>   <chr>
#>  values      : ES-AV    05  Avila 0.114     1.442       a
#>                ES-BU    09 Burgos 0.061     1.231       b
#>                ES-LE    24   Leon 0.143      1.63       c
glimpse(end)
#> #  A SpatVector 9 x 6
#> #  Geometry type: Polygons
#> #  Projected CRS: ETRS89-extended / LAEA Europe (EPSG:3035)
#> #  CRS projection units: meter <m>
#> #  Extent (x / y) : ([2,892,687 / 3,341,372] , [2,017,622 / 2,361,600])
#> 
#> $ iso2      <chr> "ES-AV", "ES-BU", "ES-LE", "ES-P", "ES-SA", "ES-SG", "ES-SO"…
#> $ cpro      <chr> "05", "09", "24", "34", "37", "40", "42", "47", "49"
#> $ name      <chr> "Avila", "Burgos", "Leon", "Palencia", "Salamanca", "Segovia…
#> $ AREA      <dbl> 0.114, 0.061, 0.143, 0.070, 0.153, 0.097, 0.062, 0.091, 0.118
#> $ PERIMETER <dbl> 1.442, 1.231, 1.630, 2.968, 2.206, 1.670, 1.547, 1.284, 1.421
#> $ letters   <chr> "a", "b", "c", "d", "e", "f", "g", "h", "i"

# Row sizes must be compatible when column-binding
try(bind_spat_cols(sv, sfobj))
#> Error in dplyr::bind_cols(alltibbs, .name_repair = .name_repair) : 
#>   Can't recycle `..1` (size 9) to match `..2` (size 100).