'How to create maximum value composite using terra r package?
I want to create a maximum value composite raster by taking maximum values from each band of a raster stack. I am using the following code
library(terra)
library(RStoolbox)
rast1 <- rast(lsat)
#Create two raster stacks
rast2 <- rast(lsat)
random_nums <- runif(length(rast2), min = 0.3, max = 0.5) # a set of random numbers the size of the image
rast2[] <- rast2[] + random_nums
rast3 <- rast(lsat)
random_nums <- runif(length(rast3), min = 0.1, max = 0.4) # a set of random numbers the size of the image
rast3[] <- rast3[] - random_nums
#Create a stack of all the rasters
s <- c(rast1, rast2, rast3)
#Create a maximum value composite
b1 <- tapp(s, index=c(1,8,15,2,9,16,3,10,17,4,11,18,5,12,19,6,13,20,7,14,21),
fun=max)
b1
Now it is giving me 21 layers. But the output should have 7 bands with each band made of by taking the maximum of rast1, rast2 and rast3 i.e. the band1 of b1 should take the maximum of band1 of rast1, 2 and 3. Likewise for b2 to b7.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
