applyCats: Apply a set of "cats" to a factor raster

View source: R/utils.R

applyCatsR Documentation

Apply a set of "cats" to a factor raster

Description

Applies a given set of "cats" (factor level ID-name pairs) to a factor raster. Any level names already shared between the given cats and the raster will be numerically remapped to the given cats IDs.

Usage

applyCats(raster, cats)

Arguments

raster

The factor SpatRast object to apply the given cats to.

cats

A data.frame that includes a "category" character column (the output of terra::cats()). S

Details

terra manages factor rasters by having each cell store an integer ID (0, 1, 2, ...) which corresponds to a level name as found in terra::cats(). However, if you load a raster with levels "A", "B", "C" and another raster with levels "B", "C", "D", terra won't detect the overlapping levels since B=1 and C=2 in the first raster and B=0 and C=1 in the second. This is important when it comes to creating prediction models that take factor rasters as input. Let's say that level "C" is a high-probability indicator of event X. If you generate a model with raster 1 the model will learn to associate X with the value C=2. If you then run the model with raster 2, the model will predict high-probability of X at cells with D=2, which is numerically correct but the level names don't match. The setRasterLevels() function solves this problem by ensuring that any level names shared between two factor rasters will also share the same integer ID.


tabrasel/WetlandTools documentation built on Dec. 20, 2024, 8:50 a.m.