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 step_add_levels
tidy(x, ...)

Arguments

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.

Value

Recipe with the new step

Examples

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)
#> named list()
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" #>