Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

oneHotEncode

One-hot encode a raster or vector


Description

Splits a categorical raster layer (or a vector) into a multilayer raster (or matrix).

Usage

oneHotEncode(img, classes, background = 0, foreground = 1,
  na.rm = FALSE, ...)

Arguments

img

RasterLayer or integer/numeric vector containing multiple classes

classes

integer: vector of classes which should be extracted

background

integer: background value (default = 0)

foreground

integer: foreground value (default = 1)

na.rm

logical: if TRUE, NAs will be coerced to the background value.

...

further arguments passed to writeRaster. Ignored if img is not a RasterLayer, but a numeric/integer vector

Value

A RasterBrick with as many layers as there are classes. Pixels matching the class of interest are set to 1, backround values by default are set to 0 (see background argument)

Examples

library(raster)

## example data
data(rlogo)
sc <- unsuperClass(rlogo, nClasses = 3)

## one-hot encode 
sc_oneHot <- oneHotEncode(sc$map, classes = c(1,2,3))

## check results
sc_oneHot
plot(sc_oneHot)

RStoolbox

Tools for Remote Sensing Data Analysis

v0.2.6
GPL (>= 3)
Authors
Benjamin Leutner [cre, aut], Ned Horning [aut], Jakob Schwalb-Willmann [aut], Robert J. Hijmans [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.