step_add_levels: Add levels to nominal variables

Description Usage Arguments Value Examples

View source: R/step_add_levels.R

Description

Add levels to nominal variables

Usage

1
2
3
4
5
step_add_levels(recipe, ..., role = NA, trained = FALSE, cols = NULL,
  levels = c("other", "missing"), skip = FALSE)

## 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?

x

A 'step_add_levels' object.

Value

Recipe with the new step

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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. 2, 2018, 1:03 a.m.