Create a Factors from A Dummy VariableSource:
step_bin2factor() creates a specification of a recipe step that will
create a two-level factor from a single dummy variable.
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 length 2 character string that indicates the factor levels for the 1's (in the first position) and the zeros (second)
Logical. Should the first level, which replaces 1's, be the factor reference level?
A character string of the selected variable names. This field is a placeholder and will be populated once
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 = TRUEas 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.
This operation may be useful for situations where a binary piece of information may need to be represented as categorical instead of numeric. For example, naive Bayes models would do better to have factor predictors so that the binomial distribution is modeled instead of a Gaussian probability density of numeric binary data. Note that the numeric data is only verified to be numeric (and does not count levels).
tidy() this step, a tibble with column
terms (the columns that will be affected) is returned.
Other dummy variable and encoding steps:
data(covers, package = "modeldata") rec <- recipe(~description, covers) %>% step_regex(description, pattern = "(rock|stony)", result = "rocks") %>% step_regex(description, pattern = "(rock|stony)", result = "more_rocks") %>% step_bin2factor(rocks) tidy(rec, number = 3) #> # A tibble: 1 × 2 #> terms id #> <chr> <chr> #> 1 rocks bin2factor_5hCeA rec <- prep(rec, training = covers) results <- bake(rec, new_data = covers) table(results$rocks, results$more_rocks) #> #> 0 1 #> yes 0 29 #> no 11 0 tidy(rec, number = 3) #> # A tibble: 1 × 2 #> terms id #> <chr> <chr> #> 1 rocks bin2factor_5hCeA