stackApply with multivariate function in R -


i looking use stackapply() in raster package , approx() linearly interpolate grid cell of 1 raster between stack of rasters. have written similar function calculation on dataframe, preform on stack of rasters rather rows in dataframe. have found previous examples of using stackapply() user defined function, none involve multiple variables.

in other words, have stack of rasters , lone raster grid (they have matching extent , resolution). want "drill" through stack, cell cell, create vector of values , linearly interpolate value of matching grid cell in lone raster stack-created vector.

my code along lines of...

require(raster) set.seed(42)  x1 <- runif(100)  x2 <- x1  x3 <- x1  x1[sample(1:100, 30)] <- na  x2[sample(1:100, 30)] <- na  x3[sample(1:100, 30)] <- na  r1 <- raster(matrix(x1, nrow=10, ncol=10))  r2 <- raster(matrix(x2, nrow=10, ncol=10))  r3 <- raster(matrix(x3, nrow=10, ncol=10))  s <- stack(r1, r2)  myfunc <- function(x,y){approx(x,c(0,1),y)} newrast <- stackapply(stack,c(1,2), fun=myfunc(s,r3)) 

i confused on how pass multiple variables fun= argument in stackapply. unsure on ind= argument. want make sure function being done through layers, rather on entire layer individually , repeated each layer.

thank you!

i think stackapply not apply here. use calc instead.

for approx-like problems, can consider raster::approxna

s <- stack(r1, r2, r3) x <- approxna(s) 

but if not work, can things

f <- function(x) x[1] * sum(x[-1]) y <- calc(s, f) 

alternatively, consider overlay

s <- stack(r1, r2) z <- overlay(s, r3, fun=function(x, y) x * y) 

(i implement function after, have no idea mean "interpolate value of matching grid cell in lone raster stack-created vector")


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -