I want to perform a neighborhood analysis in R to smooth the layer I have but keeping all the NAs of the input raster intact.
However, when I do, for instance, the following, the calculation "propagates" over the NA values - what it is an undesiderable behavior, in my case.
library(terra)
library(dplyr)
# load example raster in metric system
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f) %>%
terra::project("EPSG:32631")
# focal
neigh <- terra::focal(r, w = 7, fun = "mean")
# plot
plot(c(r, neigh))
Update:
Following the suggestion made by @dww below, I could use terra::mask. A way to deal with that, then, would be:
# focal
neigh <- terra::focal(r, w = 7, fun = "mean") %>%
terra::mask(mask = r)
# plot
plot(c(r, neigh))
Is there another way out avoid the propagation of values to NA cells within focal?
(here it is a simple example of a square filter to calculate the mean, but I am searching something that would be usefull for all types of filter, e.g. any matrix defined by terra::focalMat())
Should I deal with that when defining the weight matrix?


