[R-sig-Geo] stackApply and clusterR

joshgray joshgray at bu.edu
Wed Oct 2 14:15:02 CEST 2013


Sorry, I ignored the detail for /stackApply/ that specified that /fun/ should
have accept an na.rm argument. I also had monkeyed up the function and
indices specification. In case anyone is interested, this works:

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, r3)

beginCluster(n=8)

my.f1 <- function(x, na.rm=T){
	sum(!is.na(x))
}

s.out <- clusterR(s, stackApply, args=list(indices=rep(1, 3), fun=my.f1))

# > s[1]
#      layer.1  layer.2  layer.3
# [1,]      NA 0.914806 0.914806
# > s[2]
#        layer.1 layer.2 layer.3
# [1,] 0.4577418      NA      NA
# > s.out[1] 
# 2 
# > s.out[2]
# 1 

endCluster()



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/stackApply-and-clusterR-tp7584760p7584762.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list