Clean Categorical LevelsSource:
step_clean_levels creates a specification of a recipe step that will
clean nominal data (character or factor) so the levels consist only of
letters, numbers, and the underscore.
step_clean_levels( recipe, ..., role = NA, trained = FALSE, clean = NULL, skip = FALSE, id = rand_id("clean_levels") )
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more selector functions to choose which variables are affected by the step. See
recipes::selections()for more details.
Not used by this step since no new variables are created.
A logical to indicate if the quantities for preprocessing have been estimated.
A named character vector to clean and recode categorical levels. This is
NULLuntil computed by
recipes::prep.recipe(). Note that if the original variable is a character vector, it will be converted to a factor.
A logical. Should the step be skipped when the recipe is baked by
recipes::bake.recipe()? While all operations are baked when
recipes::prep.recipe()is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using
skip = FALSE.
A character string that is unique to this step to identify it.
An updated version of
recipe with the new step added
to the sequence of existing steps (if any).
The new levels are cleaned and then reset with
data to be processed contains novel levels (i.e., not contained in the
training set), they are converted to missing.
tidy() this step, a tibble with columns
(the selectors or variables selected),
original (the original levels) and
value (the cleaned levels) is returned.
library(recipes) library(modeldata) data(Smithsonian) smith_tr <- Smithsonian[1:15, ] smith_te <- Smithsonian[16:20, ] rec <- recipe(~., data = smith_tr) rec <- rec %>% step_clean_levels(name) rec <- prep(rec, training = smith_tr) cleaned <- bake(rec, smith_tr) tidy(rec, number = 1) #> # A tibble: 15 × 4 #> terms original value id #> <chr> <chr> <chr> <chr> #> 1 name Anacostia Community Museum anac… clea… #> 2 name Arthur M. Sackler Gallery arth… clea… #> 3 name Arts and Industries Building arts… clea… #> 4 name Cooper Hewitt, Smithsonian Design Museum coop… clea… #> 5 name Freer Gallery of Art free… clea… #> 6 name George Gustav Heye Center geor… clea… #> 7 name Hirshhorn Museum and Sculpture Garden hirs… clea… #> 8 name National Air and Space Museum nati… clea… #> 9 name National Museum of African American History and Cult… nati… clea… #> 10 name National Museum of African Art nati… clea… #> 11 name National Museum of American History nati… clea… #> 12 name National Museum of Natural History nati… clea… #> 13 name National Museum of the American Indian nati… clea… #> 14 name National Portrait Gallery nati… clea… #> 15 name Steven F. Udvar-Hazy Center stev… clea… # novel levels are replaced with missing bake(rec, smith_te) #> # A tibble: 5 × 3 #> name latitude longitude #> <fct> <dbl> <dbl> #> 1 NA 38.9 -77.0 #> 2 NA 38.9 -77.0 #> 3 NA 38.9 -77.0 #> 4 NA 38.9 -77.0 #> 5 NA 38.9 -77.1