[R-sig-Geo] Extracting raster weighted mean without NAs
Arnaud Mosnier
a.mosnier at gmail.com
Wed Sep 24 15:59:07 CEST 2014
Hi,
I need to extract the mean value of raster values into several
spatialPolygons.
I use the extract function from the raster package with the weight option
in order to take into account the area of each raster cell with data
included into each polygon.
However, I want to make the calculation even if some cells contain NAs,
Here is a small code to explain my case:
library(raster)
mat <- matrix(rep(c(4,5,3,2,1), 5), ncol=5)
r <- raster(mat)
r[2,4] <- NA
plot(r)
corners1 <- rbind(c(0.2,0.1), c(0.2,0.7), c(0.4, 0.7), c(0.4,0.1),
c(0.2,0.1))
corners2 <- rbind(c(0.7,0.1), c(0.7,0.7), c(0.9, 0.7), c(0.9,0.1),
c(0.7,0.1))
polys <- SpatialPolygons(list(Polygons(list(Polygon(corners1)), ID="1"),
Polygons(list(Polygon(corners2)), ID="2")))
plot(polys, add=T)
extract(r, polys, weight=TRUE, fun=mean, na.rm=TRUE, small=TRUE)
>[1] 2.666667 NA
Note: The use of the parameter na.rn=TRUE does not resolve my issue.
Any workaround ?
Thanks,
Arnaud
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list