
Bind multiple SpatVector, sf, sfc and data frame objects by row
Source: R/bind-rows-SpatVector.R
bind_rows.SpatVector.RdBind 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.
Arguments
- ...
Objects to combine. The first argument must be a
SpatVector. Each subsequent argument can be aSpatVector,sforsfcobject or a data frame. Columns are matched by name and any missing columns are filled withNA.- .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.
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
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
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)
#> class : SpatVector
#> geometry : polygons
#> dimensions : 4, 3 (geometries, attributes)
#> extent : 2892687, 3180130, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : cpro name iso2
#> type : <chr> <chr> <chr>
#> values : 05 NA NA
#> NA Leon ES-LE
#> NA Palencia ES-P
#> ...
# 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")
#> class : SpatVector
#> geometry : polygons
#> dimensions : 4, 4 (geometries, attributes)
#> extent : 2892687, 3180130, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : id cpro name iso2
#> type : <chr> <chr> <chr> <chr>
#> values : 1 05 NA NA
#> 2 NA Leon ES-LE
#> 2 NA Palencia ES-P
#> ...
# \donttest{
# Use with sf objects.
sfobj <- sf::st_as_sf(v2[1, ])
sfobj
#> Simple feature collection with 1 feature and 2 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 2926589 ymin: 2233673 xmax: 3125372 ymax: 2361600
#> Projected CRS: ETRS89-extended / LAEA Europe
#> name iso2 geometry
#> 1 Leon ES-LE POLYGON ((3049427 2233673, ...
bind_spat_rows(v1, sfobj)
#> class : SpatVector
#> geometry : polygons
#> dimensions : 2, 3 (geometries, attributes)
#> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : cpro name iso2
#> type : <chr> <chr> <chr>
#> values : 05 NA NA
#> NA Leon ES-LE
# Reproject with a message when the CRS differs.
sfobj_3857 <- as_spatvector(sfobj) |> project("EPSG:3857")
bind_spat_rows(v1, sfobj_3857)
#> ! Reprojecting object 2 in `...` because it does not have the same CRS as object 1.
#> class : SpatVector
#> geometry : polygons
#> dimensions : 2, 3 (geometries, attributes)
#> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : cpro name iso2
#> type : <chr> <chr> <chr>
#> values : 05 NA NA
#> NA Leon ES-LE
# Bind data frames with a message.
data("mtcars")
bind_spat_rows(v1, sfobj, mtcars, .id = "id2")
#> ! Object 3 in `...` is <data.frame>
#> The result includes empty geometries.
#> class : SpatVector
#> geometry : polygons
#> dimensions : 34, 15 (geometries, attributes)
#> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : id2 cpro name iso2 mpg cyl disp hp drat wt (and 5 more)
#> type : <chr> <chr> <chr> <chr> <num> <num> <num> <num> <num> <num>
#> values : 1 05 NA NA NA NA NA NA NA NA
#> 2 NA Leon ES-LE NA NA NA NA NA NA
#> 3 NA NA NA 21 6 160 110 3.9 2.62
#> ...
# Use lists
bind_spat_rows(list(v1[1, ], sfobj[1:2, ]))
#> class : SpatVector
#> geometry : polygons
#> dimensions : 3, 3 (geometries, attributes)
#> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : cpro name iso2
#> type : <chr> <chr> <chr>
#> values : 05 NA NA
#> NA Leon ES-LE
#> NA NA NA
# Use a named list with `.id`.
bind_spat_rows(list(
SpatVector = v1[1, ], sf = sfobj[1, ],
mtcars = mtcars[1, ]
), .id = "source")
#> ! Object 3 in `...` is <data.frame>
#> The result includes empty geometries.
#> class : SpatVector
#> geometry : polygons
#> dimensions : 3, 15 (geometries, attributes)
#> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : source cpro name iso2 mpg cyl disp hp drat wt (and 5 more)
#> type : <chr> <chr> <chr> <chr> <num> <num> <num> <num> <num> <num>
#> values : SpatVector 05 NA NA NA NA NA NA NA NA
#> sf NA Leon ES-LE NA NA NA NA NA NA
#> mtcars NA NA NA 21 6 160 110 3.9 2.62
# }