jeudi 4 octobre 2018

How to know from which raster a value obtained though a conditional function comes from in R

I have a raster stack composed of three rasters. I created a function that create a new raster based on several conditions among the rasters in the stack:

library(raster)

r1= raster(nrows = 1, ncols = 1, res = 0.5,xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = 0.3)

r2= raster(nrows = 1, ncols = 1, res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5,vals = 0.7)

r3= raster(nrows = 1, ncols = 1, res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = 0.1)

s<- stack(r1, r2, r3)

#Creation of the function
f <- function(x1,x2, x3) {
ifelse( 0 <= x3 & x3< x1, x1, ifelse( x1 <= x3 & x3< x1 +x2, x2) )
}

#Creation of the final raster
r <- overlay(s, fun=f)

This function gives me, depending on the values of r3, a new raster r with the values coming from r1 or r2. Instead of having the cell values of the rasters, I would like the new raster to indicate from which raster the condition is verified.

Aucun commentaire:

Enregistrer un commentaire