oneHot: One hot encoding

View source: R/oneHot.R

oneHotR Documentation

One hot encoding

Description

One hot encode a vector or factors in a data.frame

Usage

oneHot(x, xname = NULL, verbose = FALSE)

## Default S3 method:
oneHot(x, xname = NULL, verbose = TRUE)

## S3 method for class 'data.frame'
oneHot(x, xname = NULL, verbose = TRUE)

## S3 method for class 'data.table'
oneHot(x, xname = NULL, verbose = TRUE)

dt_set_oneHot(x, xname = NULL, verbose = TRUE)

Arguments

x

Vector or data.frame

xname

Character: Variable name

verbose

Logical: If TRUE, print messages to console

Details

A vector input will be one-hot encoded regardless of type by looking at all unique values. With data.frame input, only column of type factor will be one-hot encoded. This function is used by preprocess. oneHot.data.table operates on a copy of its input. oneHot_ performs one-hot encoding in-place.

Value

For vector input, a one-hot-encoded matrix, for data.frame frame input, an expanded data.frame where all factors are one-hot encoded

Author(s)

E.D. Gennatas

Examples

## Not run: 
iris_oh <- oneHot(iris)
# factor with only one unique value but 2 levels:
vf <- factor(rep("alpha", 20), levels = c("alpha", "beta"))
vf_onehot <- oneHot(vf)

## End(Not run)
oneHot(iris) |> head()
ir <- data.table::as.data.table(iris)
ir_oh <- oneHot(ir)
ir_oh
ir <- data.table::as.data.table(iris)
# dt_set_oneHot operates in-place; therefore no assignment is used:
dt_set_oneHot(ir)
ir

egenn/rtemis documentation built on Oct. 28, 2024, 6:30 a.m.