[R-sig-Geo] Apply a different threshold for each layer in a brick object

Agustin Lobo alobolistas at gmail.com
Fri Oct 23 13:16:18 CEST 2015


Hi!

Given
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
s <- brick(r,r,r,r,r,r)
s <- s * 1:6
summary(s)

applying a common threshold is simple, i.e.
th <- 50
s[s>th] <- th

but what if th is a vector with a different value for each layer?
Is there a faster (raster!) way (i.e. using calc() or stackApply() )
for the following:

s2 <- brick(r,r,r,r,r,r)
s2 <- s2 * 1:6
th <- c(50*(1:6))
for (i in 1:nlayers(s)){
  a <- s2[[i]]
  a[a>th[i]] <- th[i]
  s2[[i]]  <- a
}

Tried with calc() and stackApply(), but get errors.
Thanks

Agus



More information about the R-sig-Geo mailing list