Is there a way to do this but change xout in approx to the cell values of another raster? I've been trying to get two different rasters into app and it won't seem to work for me.
I'm looking to interpolate values to an depth, where each layer in the rast would be the climate value at a fixed depth. Going from surface to the seafloor. The output would be a single layer raster where the stack cell level values are interpolated to the nearest depth based on depth of input raster z
.
I was thinking something like this:
library(raster)
r <- raster(nrows=10, ncols=10);
x1 <- setValues(r, runif(ncell(r)))
x16 <- setValues(r, runif(ncell(r))) + 10
x30 <- setValues(r, runif(ncell(r))) + 20
## this would be say elevations per cell
z <- setValues(r,sample(1:30,100,replace=TRUE))
s <- stack(x1, x16, x30)
x <- calc(s, fun=function(y) approx(x=c(1,16,30), y=y, xout=z)$y)
I've tried various ways to index the depth raster z
with no luck. Any pointers would be great.