Skip to content

step_integer creates a specification of a recipe step that will convert new data into a set of integers based on the original data values.


  role = "predictor",
  trained = FALSE,
  strict = FALSE,
  zero_based = FALSE,
  key = NULL,
  skip = FALSE,
  id = rand_id("integer")



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.


For model terms created by this step, what analysis role should they be assigned? By default, the new columns created by this step from the original variables will be used as predictors in a model.


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


A logical for whether the values should be returned as integers (as opposed to double).


A logical for whether the integers should start at zero and new values be appended as the largest integer.


A list that contains the information needed to create integer variables for each variable contained in terms. This is NULL until the step is trained 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.


step_integer will determine the unique values of each variable from the training set (excluding missing values), order them, and then assign integers to each value. When baked, each data point is translated to its corresponding integer or a value of zero for yet unseen data (although see the zero_based argument above). Missing values propagate.

Factor inputs are ordered by their levels. All others are ordered by sort.

Despite the name, the new values are returned as numeric unless strict = TRUE, which will coerce the results to integers.


When you tidy() this step, a tibble with columns terms (the selectors or variables selected) and value (a list column with the conversion key) is returned.



okc$location <- factor(okc$location)

okc_tr <- okc[1:100, ]
okc_tr$age[1] <- NA

okc_te <- okc[101:105, ]
okc_te$age[1] <- NA
okc_te$diet[1] <- "fast food"
okc_te$diet[2] <- NA

rec <- recipe(Class ~ ., data = okc_tr) %>%
  step_integer(all_predictors()) %>%
  prep(training = okc_tr)

bake(rec, okc_te, all_predictors())
#> # A tibble: 5 × 5
#>     age  diet height location  date
#>   <dbl> <dbl>  <dbl>    <dbl> <dbl>
#> 1    NA     0     12      102    37
#> 2    11    NA      9       25     0
#> 3     0     2     10       31    36
#> 4    20     3     11      102     0
#> 5    12     1      4      102    33
tidy(rec, number = 1)
#> # A tibble: 5 × 3
#>   terms    value              id           
#>   <chr>    <list>             <chr>        
#> 1 age      <tibble [23 × 2]>  integer_3IckW
#> 2 diet     <tibble [8 × 2]>   integer_3IckW
#> 3 height   <tibble [18 × 2]>  integer_3IckW
#> 4 location <tibble [135 × 2]> integer_3IckW
#> 5 date     <tibble [38 × 2]>  integer_3IckW