step_add_levels.Rd
Add levels to nominal variables
step_add_levels(recipe, ..., role = NA, trained = FALSE, cols = NULL, levels = c("other", "missing"), skip = FALSE) # S3 method for 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? |
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)#> $has_missing #> [1] "b" #> #> $has_rare #> [1] "common" "rare" #> #> $has_both #> [1] "common" "rare" #> #> $has_neither #> [1] "cat1" "cat2" #>lapply(baked[, sapply(baked, is.factor)], levels)#> $has_missing #> [1] "b" "other" "missing" #> #> $has_rare #> [1] "common" "rare" "other" "missing" #> #> $has_both #> [1] "common" "rare" "other" "missing" #> #> $has_neither #> [1] "cat1" "cat2" "other" "missing" #>