[R-sig-Geo] Extracting raster weighted mean without NAs

Pascal Oettli kridox at ymail.com
Wed Sep 24 16:19:20 CEST 2014


Hi Arnaud,

It works for me with raster version 2.3-0. Please provide the output
of sessionInfo().

                [,1]
[1,] 2.666667
[2,] 2.454545

Regards,
Pascal

On Wed, Sep 24, 2014 at 10:59 PM, Arnaud Mosnier <a.mosnier at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan



More information about the R-sig-Geo mailing list