
Last chance! 50% off unlimited learning
Sale ends in
Create a new SpatRaster object, by combining two or more SpatRaster objects.
You should supply a function fun
to determine how the SpatRasters are combined. The number of arguments in the function must match the number of SpatRaster objects (or take any number). For example, if you combine two SpatRaster objects you could use multiply: fun=function(x,y){return(x*y)}
percentage: fun=function(x,y){return(100 * x / y)}
. If you combine three layers you could use fun=function(x,y,z){return((x + y) * z)}
Note that the function must work for vectors (not only for single numbers). That is, it must return the same number of elements as its input vectors. Alternatively, you can also supply a function such as sum
, that takes n
arguments.
# S4 method for SpatRaster,SpatRaster
overlay(x, y, fun, ..., filename="", overwrite=FALSE, wopt=list())
SpatRaster
SpatRaster
Function to be applied. The number of arguments of the function should match the number of objects, or it should take any number of arguments. When using multi-layer objects the function should match the number of layers of the object (unless unstack=FALSE)
Additional arguments to fun
character. Output filename. Optional
logical. If TRUE
, filename
is overwritten
list. Options for writing files as in writeRaster
SpatRaster
# NOT RUN {
r1 <- rast(ncol=10, nrow=10)
r2 <- rast(ncol=10, nrow=10)
values(r1) <- 1:ncell(r1)
values(r2) <- ncell(r1):1
x <- overlay(r1, r2, fun=function(x,y){return(x+y)})
# }
Run the code above in your browser using DataLab