Learn R Programming

terra (version 0.8-5)

factors: Factors

Description

These functions allow for defining a SpatRaster layer as a categorical variable. The cell values are an index (id), whereas the actual values are stored seperately, in a table (sometimes called "Raster Attribute Table"). This table is a data.frame. The first column in the RAT ("ID") has the unique cell values of the layer; this column should normally not be changed. The other columns can be of any basic type (factor, character, integer, numeric or logical).

Function 'levels' returns the RAT for inspection. It can be modified and set using levels(x) <- value.

as.factor and ratify create a layer with a RAT table. deratify creates a single layer for a (or each) variable in the RAT table.

Usage

is.factor(x)
as.factor(x)
levels(x)
# S4 method for SpatRaster
rats(x)
setRat(x, layer, rat)

Arguments

x

SpatRaster

layer

layer number

rat

data.frame

Value

SpatRaster; list (levels); boolean (is.factor)

Examples

Run this code
# NOT RUN {
set.seed(0)
r <- rast(nrow=10, ncol=10)
values(r) <- sample(3, ncell(r), replace=TRUE) + 2
is.factor(r)
cls <- c("forest", "water", "urban")
levels(r) <- cls



f <- as.factor(r)
is.factor(f)
# }

Run the code above in your browser using DataLab