I am trying to calculate a 72 bands raster. If the first 36 bands value (near infrared band) is greater than the latter 36 bands value (shortwave infrared band), assign it 0; if not then proceed on the following function. I tried other ways of writing it (basically the same logic) and the same errors show up. This is the function I wrote:
raster_stack <- stack("NIR.bin", "SWIR.bin")
#ndii = NIR - SWIR/NIR+SWIR
fun <- function(x) {
x[is.na(x)] <- 0;
if (x[37:72] >= x[1:36]){
0} else {
ndii <- ((x[1:36]-x[37:72]) / (x[1:36]+x[37:72]));
silent=TRUE;
return(ndii)
}
}
ndii <- calc(raster_stack, fun)
The error message is always this one:
Error in setValues(out, x) : values must be numeric, integer, logical or factor
I added x[is.na(x)] <- 0
to get rid of the NA value, but it seems that it does not help. Any insights on solving this?