Merge SpatRasters to form a new SpatRaster object with a larger spatial extent. If objects overlap, the values get priority in the same order as the arguments. The SpatRasters must have the same origin and spatial resolution. In areas where the SpatRaster objects overlap, the values of the SpatRaster that is last in the sequence of arguments will be retained. See classify
to merge a SpatRaster
and a data.frame
. You can also merge SpatExtent objects.
There is a also a method for merging SpatVector with a data.frame; that is, to join the data.frame to the attribute table of the SpatVector.
# S4 method for SpatRaster,SpatRaster
merge(x, y, ..., filename="", overwrite=FALSE, wopt=list())# S4 method for SpatRasterCollection,missing
merge(x, filename="", ...)
# S4 method for SpatExtent,SpatExtent
merge(x, y, ...)
# S4 method for SpatVector,data.frame
merge(x, y, ...)
SpatRaster or SpatExtent
SpatRaster or SpatExtent
object of same class as x
if x
is a SpatRaster: additional objects of the same class as x
. If x
is a SpatRasterCollection: options for writing files as in writeRaster
. If x
is a SpatVector, the same arguments as in merge
character. Output filename
logical. If TRUE
, filename
is overwritten
list with named options for writing files as in writeRaster
Combining tiles with vrt
may be more efficient. See mosaic
for averaging overlapping regions.
x <- rast(xmin=-110, xmax=-80, ymin=40, ymax=70, ncols=30, nrows=30)
y <- rast(xmin=-85, xmax=-55, ymax=60, ymin=30, ncols=30, nrows=30)
z <- rast(xmin=-60, xmax=-30, ymax=50, ymin=20, ncols=30, nrows=30)
values(x) <- 1:ncell(x)
values(y) <- 1:ncell(y)
values(z) <- 1:ncell(z)
m1 <- merge(x, y, z)
m2 <- merge(z, y, x)
m3 <- merge(y, x, z)
# if you have many SpatRasters make a SpatRasterCollection from a list
rlist <- list(x, y, z)
rsrc <- sprc(rlist)
m <- merge(rsrc)
## SpatVector with data.frame
f <- system.file("ex/lux.shp", package="terra")
p <- vect(f)
dfr <- data.frame(District=p$NAME_1, Canton=p$NAME_2, Value=round(runif(length(p), 100, 1000)))
dfr <- dfr[1:5, ]
pm <- merge(p, dfr, all.x=TRUE, by.x=c('NAME_1', 'NAME_2'), by.y=c('District', 'Canton'))
pm
values(pm)
Run the code above in your browser using DataLab