View source: R/step_add_levels.R
step_add_levels | R Documentation |
Add levels to nominal variables
step_add_levels( recipe, ..., role = NA, trained = FALSE, cols = NULL, levels = c("other", "missing"), skip = FALSE, id = rand_id("bagimpute") ) ## S3 method for class 'step_add_levels' tidy(x, ...)
recipe |
recipe object. This step will be added |
... |
One or more selector functions |
role |
Ought to be nominal |
trained |
Has the recipe been prepped? |
cols |
columns to be prepped |
levels |
Factor levels to add to variables. Default = c("other", "missing") |
skip |
A logical. Should the step be skipped when the recipe is baked? |
id |
a unique step id that will be used to unprep |
x |
A 'step_add_levels' object. |
Recipe with the new step
library(recipes) d <- data.frame(num = 1:30, has_missing = c(rep(NA, 10), rep('b', 20)), has_rare = c("rare", rep("common", 29)), has_both = c("rare", NA, rep("common", 28)), has_neither = c(rep("cat1", 15), rep("cat2", 15))) rec <- recipe( ~ ., d) %>% step_add_levels(all_nominal()) %>% prep(training = d) baked <- bake(rec, d) lapply(d[, sapply(d, is.factor)], levels) lapply(baked[, sapply(baked, is.factor)], levels)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.