Learn R Programming

prioritizr (version 8.0.6)

binary_stack: Binary stack

Description

Convert a single-layer terra::rast() object that contains integer values into a multi-layer terra::rast() object with pixel values denote the presence/absence of a given integer value. This is methodology is also known as "one-hot encoding".

Usage

binary_stack(x, keep_all = TRUE)

# S3 method for Raster binary_stack(x, keep_all = TRUE)

# S3 method for SpatRaster binary_stack(x, keep_all = TRUE)

Value

A terra::rast() object.

Arguments

x

terra::rast() object with a single layer that contains integer values.

keep_all

logical value indicating if all integers should be kept in the output. If TRUE, the output will contain a layer for each sequential integer between 1 and the maximum value in x. If FALSE, the output will only contain layers for integer values present in x. Defaults to TRUE.

Details

This function is provided to help manage data that encompass multiple management zones. For instance, this function may be helpful for preparing raster data for add_locked_in_constraints() and add_locked_out_constraints() since they require binary rasters as input arguments. It is essentially a wrapper for terra::segregate(). Note that this function assumes x contains integer values.

See Also

category_layer(), terra::segregate().

Examples

Run this code
# create raster with categorical values
x <- terra::rast(matrix(c(1, 2, 4, 0, NA, 1), nrow = 3))

# plot the raster
if (FALSE) {
plot(x, main = "x")
}

# convert to binary stack
y <- binary_stack(x)

# plot result
if (FALSE) {
plot(y)
}

Run the code above in your browser using DataLab