oneHot | R Documentation |
One hot encode a vector or factors in a data.frame
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)
x |
Vector or data.frame |
xname |
Character: Variable name |
verbose |
Logical: If TRUE, print messages to console |
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.
For vector input, a one-hot-encoded matrix, for data.frame frame input, an expanded data.frame where all factors are one-hot encoded
E.D. Gennatas
## 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.