hunzikp / velox

https://hunzikp.github.io/velox/
119 stars 23 forks source link

Enable na.rm=TRUE for aggregate #9

Open MatthieuStigler opened 7 years ago

MatthieuStigler commented 7 years ago

It looks like aggregate is not allowing for NAs? It would be great if it were possible to have a na.rm=TRUE argument.

mat <- matrix(c(1,2,3,NA), 2, 2)
vx <- velox(mat, extent=c(0,1,0,1), res=c(0.5,0.5), crs="+proj=longlat +datum=WGS84 +no_defs")
plot(vx$as.RasterLayer())

vx$aggregate(factor=c(2,2), aggtype="sum")

Thanks!

nizimmer commented 7 years ago

Hey @MatthieuStigler ,

according to https://gis.stackexchange.com/questions/236250/function-argument-for-all-values-in-r-raster-extract-and-velox-raster-extract

you can solve this issue by using

fun=function(x){mean(x, na.rm=TRUE)}

as an argument. For me its working.

MatthieuStigler commented 7 years ago

Oh thanks Nicolas! But I think the post refers to the extract function, not necessarily the aggregate one?

ErvanCH commented 4 years ago

Hey @MatthieuStigler ,

Coming a bit late I assume, but I am facing the same problem and passing a function, such as fun=function(x){mean(x, na.rm=TRUE)}, doesn't work. An alternative would be to create a raster where NA are replaced by 0. If you work with sf-objects you could simply use: polygon_rasterized <- fasterize::fasterize(sf,raster,f,background=0) then transform into velox object.

see24 commented 4 years ago

Having an na.rm option would also be great for the meanFocal function