step_add_levels: Add levels to nominal variables

View source: R/step_add_levels.R

step_add_levelsR Documentation

Add levels to nominal variables

Description

Add levels to nominal variables

Usage

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, ...)

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)
lapply(baked[, sapply(baked, is.factor)], levels)

healthcareai documentation built on Sept. 5, 2022, 5:12 p.m.