# Fast Zonal Statistics to replace velox?

Does anyone know an equivalent package for the `velox` package for fast zonal statistics in R? This package was by far the best performing tool when working with large raster objects but has been recently removed from CRAN...

• You can still install it from the source code on github - or at least try, assuming its not come off CRAN because it doesn't work. – Spacedman Jun 1 at 7:54

I have moved to using the `exact_extract` function in the `exactextractr` package. One nice thing is that it returns all cell intersections with the fractional proportion of each cells intersection. You could use the proportion of intersection column as weights or to filter out small intersections.

Also, there are numerous statistics available, including some weighted ones, that are part of the C++ code, so very fast. The package takes `sf` class polygons and `raster` class raster data.

You can use the `mmand` package

Example:

``````#random sampling points:

dsn = ".",
layer = "random_points",
encoding = "utf8")

pts\$id <- as.numeric(pts\$id)

pts <- as.data.frame(pts)

library(mmand)
library(raster)

r1 = raster("G:/romain/imperviousness_project/SWI.tif")

#Let's rasterize the points based on their x y coordinates:

z <- rasterize(pts[,2:3], r1, field = pts\$id)

#I want to get statistic from a zone of 3*3 pixels around my point (in my example 1 pixel = 5 meters so the total area is 15m2):

kern <- shapeKernel(c(3,3), type="box")
z[,] <- dilate(as.matrix(z), kern)

plot(z)

zonal(r1, z, fun = "mean")
``````
• Do you understand what the `mmand` package is actually doing here? The `shapeKernel` and `dialate` functions are from `mmand` and are just used in creating a 3x3 matrix around each point that is then rasterized and passed to the `raster::zonal` function. In this case the `mmand` package has very little to do with calculating zonal statistics and is completely irrelevant outside of the point example (ie., cannot be expanded to polygons). This also creates a huge amount of unnecessary overhead and would be massively slower than just using `raster::extract`. – Jeffrey Evans Jun 1 at 14:55
• Ok sorry I didn't know, I only used it once for points ! My bad ! – wanderzen Jun 1 at 15:15
• No worries, live and learn. – Jeffrey Evans Jun 1 at 15:23