I am using the terra::focal
function to fill missing data in a categorical raster. The problem is that the result of the focal
function gives a raster with numerical values. I would really like to preserve the original categories, which are formatted like this "1.1.1" (i.e., numbers between points). Is there a way to do this to keep the categories? I have provided a reproducible example below. For your information, here are the dimensions of my actual raster: 258, 600, 1 (nrow, ncol, nlyr).
set.seed(1)
## Build a raster with categories
r <- terra::rast(matrix(1:25, ncol = 5), crs = "EPSG:3857")
as_tbl <- tibble::as_tibble(r, xy = TRUE)
as_tbl$lyr.1 <- as.factor(paste0(sample(1:10, size = dim(as_tbl)[1], replace = TRUE), ".", sample(1:10, size = dim(as_tbl)[1], replace = TRUE), ".", sample(1:10, size = dim(as_tbl)[1], replace = TRUE)))
newrast <- tidyterra::as_spatraster(as_tbl, crs = "EPSG:3857")
print(newrast)
## Replace cells with NA
newrast[2,2] <- NA
newrast[3,4] <- NA
## plot(newrast)
## Appy the focal function
test <- terra::focal(newrast, w = matrix(1, nrow = 3, ncol = 3), fun = "modal", na.policy = "only", na.rm = TRUE)
print(test)
## plot(test)
I am using the terra::focal
function to fill missing data in a categorical raster. The problem is that the result of the focal
function gives a raster with numerical values. I would really like to preserve the original categories, which are formatted like this "1.1.1" (i.e., numbers between points). Is there a way to do this to keep the categories? I have provided a reproducible example below. For your information, here are the dimensions of my actual raster: 258, 600, 1 (nrow, ncol, nlyr).
set.seed(1)
## Build a raster with categories
r <- terra::rast(matrix(1:25, ncol = 5), crs = "EPSG:3857")
as_tbl <- tibble::as_tibble(r, xy = TRUE)
as_tbl$lyr.1 <- as.factor(paste0(sample(1:10, size = dim(as_tbl)[1], replace = TRUE), ".", sample(1:10, size = dim(as_tbl)[1], replace = TRUE), ".", sample(1:10, size = dim(as_tbl)[1], replace = TRUE)))
newrast <- tidyterra::as_spatraster(as_tbl, crs = "EPSG:3857")
print(newrast)
## Replace cells with NA
newrast[2,2] <- NA
newrast[3,4] <- NA
## plot(newrast)
## Appy the focal function
test <- terra::focal(newrast, w = matrix(1, nrow = 3, ncol = 3), fun = "modal", na.policy = "only", na.rm = TRUE)
print(test)
## plot(test)
Share
Improve this question
asked yesterday
Sophie PèreSophie Père
591 silver badge5 bronze badges
1 Answer
Reset to default 1With these example data
library(terra)
set.seed(1)
r <- terra::rast(nrow=5, ncol=5, vals=sample(c(LETTERS[1:5], NA), 25, replace=TRUE))
You can do
test <- terra::focal(r, w=3, fun="modal", na.policy="only", na.rm=TRUE)
levels(test) <- levels(r)
test
#class : SpatRaster
#dimensions : 5, 5, 1 (nrow, ncol, nlyr)
#resolution : 72, 36 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
#source(s) : memory
#categories : letter
#name : letter
#min value : A
#max value : E
In "terra" version 1.8-26 (currently the development version), the categories are now kept automatically if you use focal
with "fun=modal", "min", "max", or "first". You can install the development version with
install.packages('terra', repos='https://rspatial.r-universe.dev')