I have a raster which I applied a Gaussian filter. By "default", the Gaussian filter, takes the mean within the kernel. How can I take the max, instead of the mean, within the kernel?

Here is how I applied the Gaussian filter:


wd = "test/"

dem = rast(paste0(wd, "dem.tif")) # my original raster

gf <- focalMat(dem, 0.5*30, "Gauss")
r_gf <- focal(dem, w = gf, na.rm = TRUE)

The raster I am using:

class       : SpatRaster 
dimensions  : 487, 538, 1  (nrow, ncol, nlyr)
resolution  : 29.97561, 29.97126  (x, y)
extent      : 330330.3, 346457.2, 4451378, 4465974  (xmin, xmax, ymin, ymax)
coord. ref. : GGRS87 / Greek Grid (EPSG:2100) 
source      : dem.tif 
name        :       dem 
min value   :  144.7688 
max value   : 2169.7751 

I am using R 4.3.1 and RStudio 2023.06.1 Build 524.

1 Answer 1


Did you try setting the argument fun="max" inside the focal() function? See this section from the terra docs.

enter image description here

  • I wasn't expecting it to be that easy.
    – Nikos
    Aug 27, 2023 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.