jeudi 11 mars 2021

ifelse returning attempt to replicate an object of type 'S4' error in R

I am trying to set the values of E raster as 0.987 where b is over 0.5, otherwise use whatever was in E. For that, I am using the following code

a <- raster::raster(ncol=100, nrow=100)
set.seed(2)
raster::values(a) = runif(10000, min=0.1, max=0.4)
b <- raster::raster(ncol=100, nrow=100)
set.seed(2)
raster::values(b) = runif(10000, min=0.02, max=0.8)

c <- ((b - 0.2)/(0.5 - 0.2))^2
dE <- (1 - 0.9747)*(1 - c)*0.55*0.9896
E <- raster::raster(b)
E[] = ifelse(b[]>0.5, 0.989+dE, E[])

which returns me following error

Error in rep(yes, length.out = len) : attempt to replicate an object of type 'S4'

How can I fix this error?

Aucun commentaire:

Enregistrer un commentaire