step_unknown()
creates a specification of a recipe step that will assign
a missing value in a factor level to "unknown"
.
step_unknown(
recipe,
...,
role = NA,
trained = FALSE,
new_level = "unknown",
objects = NULL,
skip = FALSE,
id = rand_id("unknown")
)
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more selector functions to choose variables for this step.
See 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 single character value that will be assigned to new factor levels.
A list of objects that contain the information on factor
levels that will be determined by prep()
.
A logical. Should the step be skipped when the recipe is baked by
bake()
? While all operations are baked when prep()
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 = TRUE
as it
may affect the computations for subsequent operations.
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 any existing operations.
The selected variables are adjusted to have a new level (given by
new_level
) that is placed in the last position.
Note that if the original columns are character, they will be converted to factors by this step.
If new_level
is already in the data given to prep()
, an error is thrown.
When you tidy()
this step, a tibble is returned with
columns terms
, value
, and id
:
character, the selectors or variables selected
character, the factor levels for the new values
character, id of this step
The underlying operation does not allow for case weights.
Other dummy variable and encoding steps:
step_bin2factor()
,
step_count()
,
step_date()
,
step_dummy()
,
step_dummy_extract()
,
step_dummy_multi_choice()
,
step_factor2string()
,
step_holiday()
,
step_indicate_na()
,
step_integer()
,
step_novel()
,
step_num2factor()
,
step_ordinalscore()
,
step_other()
,
step_regex()
,
step_relevel()
,
step_string2factor()
,
step_time()
,
step_unorder()
data(Sacramento, package = "modeldata")
rec <-
recipe(~ city + zip, data = Sacramento) |>
step_unknown(city, new_level = "unknown city") |>
step_unknown(zip, new_level = "unknown zip") |>
prep()
table(bake(rec, new_data = NULL) |> pull(city),
Sacramento |> pull(city),
useNA = "always"
) |>
as.data.frame() |>
dplyr::filter(Freq > 0)
#> Var1 Var2 Freq
#> 1 ANTELOPE ANTELOPE 33
#> 2 AUBURN AUBURN 5
#> 3 CAMERON_PARK CAMERON_PARK 9
#> 4 CARMICHAEL CARMICHAEL 20
#> 5 CITRUS_HEIGHTS CITRUS_HEIGHTS 35
#> 6 COOL COOL 1
#> 7 DIAMOND_SPRINGS DIAMOND_SPRINGS 1
#> 8 EL_DORADO EL_DORADO 2
#> 9 EL_DORADO_HILLS EL_DORADO_HILLS 23
#> 10 ELK_GROVE ELK_GROVE 114
#> 11 ELVERTA ELVERTA 4
#> 12 FAIR_OAKS FAIR_OAKS 9
#> 13 FOLSOM FOLSOM 17
#> 14 FORESTHILL FORESTHILL 1
#> 15 GALT GALT 21
#> 16 GARDEN_VALLEY GARDEN_VALLEY 1
#> 17 GOLD_RIVER GOLD_RIVER 4
#> 18 GRANITE_BAY GRANITE_BAY 3
#> 19 GREENWOOD GREENWOOD 1
#> 20 LINCOLN LINCOLN 22
#> 21 LOOMIS LOOMIS 2
#> 22 MATHER MATHER 1
#> 23 MEADOW_VISTA MEADOW_VISTA 1
#> 24 NORTH_HIGHLANDS NORTH_HIGHLANDS 21
#> 25 ORANGEVALE ORANGEVALE 11
#> 26 PENRYN PENRYN 1
#> 27 PLACERVILLE PLACERVILLE 10
#> 28 POLLOCK_PINES POLLOCK_PINES 3
#> 29 RANCHO_CORDOVA RANCHO_CORDOVA 28
#> 30 RANCHO_MURIETA RANCHO_MURIETA 3
#> 31 RIO_LINDA RIO_LINDA 13
#> 32 ROCKLIN ROCKLIN 17
#> 33 ROSEVILLE ROSEVILLE 48
#> 34 SACRAMENTO SACRAMENTO 438
#> 35 WALNUT_GROVE WALNUT_GROVE 1
#> 36 WEST_SACRAMENTO WEST_SACRAMENTO 3
#> 37 WILTON WILTON 5
tidy(rec, number = 1)
#> # A tibble: 1 × 3
#> terms value id
#> <chr> <chr> <chr>
#> 1 city unknown city unknown_NRDyG