To combine several SpatRasters using the sum function in the R package Terra, follow these steps:
library(terra)
raster1 <- rast("raster1.tif")
raster2 <- rast("raster2.tif")
terra::stack
function to create a new SpatRaster stack:raster_stack <- stack(raster1, raster2)
terra::apply
function with the sum
function to sum the values of each pixel across all the SpatRasters in the stack:raster_sum <- apply(raster_stack, 1, sum)
This will create a new SpatRaster raster_sum
with the same dimensions and resolution as the original rasters, but with the values of each pixel equal to the sum of the values of that pixel across all the SpatRasters in the stack.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-15 07:12:00 +0000
Seen: 9 times
Last updated: May 15 '23
What is the process of using the Multmerge() function in r to combine files in a directory?
Is it possible for two distinct useState functions to trigger changes simultaneously?
How can one use node to change an ogg file into an mp3 file?
How can a function pointer be transferred between different files in CUDA?
What does the error message "TypeError: e.endsWith is not a function" mean in Redoc?
What is the expected outcome of the math.floor function in terms of the return value?