Skip to content

step_unknown creates a specification of a recipe step that will assign a missing value in a factor level to"unknown".

Usage

step_unknown(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  new_level = "unknown",
  objects = NULL,
  skip = FALSE,
  id = rand_id("unknown")
)

Arguments

recipe

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.

role

Not used by this step since no new variables are created.

trained

A logical to indicate if the quantities for preprocessing have been estimated.

new_level

A single character value that will be assigned to new factor levels.

objects

A list of objects that contain the information on factor levels that will be determined by prep().

skip

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.

id

A character string that is unique to this step to identify it.

Value

An updated version of recipe with the new step added to the sequence of any existing operations.

Details

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.

Tidying

When you tidy() this step, a tibble with columns terms (the columns that will be affected) and value (the factor levels that is used for the new value) is returned.

Case weights

The underlying operation does not allow for case weights.

Examples

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