[R-sig-Geo] Extracting raster weighted mean without NAs
a.mosnier at gmail.com
Wed Sep 24 15:59:07 CEST 2014
I need to extract the mean value of raster values into several
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:
mat <- matrix(rep(c(4,5,3,2,1), 5), ncol=5)
r <- raster(mat)
r[2,4] <- NA
corners1 <- rbind(c(0.2,0.1), c(0.2,0.7), c(0.4, 0.7), c(0.4,0.1),
corners2 <- rbind(c(0.7,0.1), c(0.7,0.7), c(0.9, 0.7), c(0.9,0.1),
polys <- SpatialPolygons(list(Polygons(list(Polygon(corners1)), ID="1"),
extract(r, polys, weight=TRUE, fun=mean, na.rm=TRUE, small=TRUE)
> 2.666667 NA
Note: The use of the parameter na.rn=TRUE does not resolve my issue.
Any workaround ?
[[alternative HTML version deleted]]
More information about the R-sig-Geo