Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

step_bin2factor

Create a Factors from A Dummy Variable


Description

step_bin2factor creates a specification of a recipe step that will create a two-level factor from a single dummy variable.

Usage

step_bin2factor(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  levels = c("yes", "no"),
  ref_first = TRUE,
  columns = NULL,
  skip = FALSE,
  id = rand_id("bin2factor")
)

## S3 method for class 'step_bin2factor'
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

Selector functions that choose which variables will be converted. See selections() for more details. For the tidy method, these are not currently used.

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.

levels

A length 2 character string that indicates the factor levels for the 1's (in the first position) and the zeros (second)

ref_first

Logical. Should the first level, which replaces 1's, be the factor reference level?

columns

A vector with the selected variable names. This is NULL until computed by prep.recipe().

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when 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 = TRUE as it may affect the computations for subsequent operations

id

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

x

A step_bin2factor object.

Details

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).

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any). For the tidy method, a tibble with columns terms (the columns that will be affected).

Examples

library(modeldata)
data(covers)

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)

rec <- prep(rec, training = covers)
results <- bake(rec, new_data = covers)

table(results$rocks, results$more_rocks)

tidy(rec, number = 3)

recipes

Preprocessing Tools to Create Design Matrices

v0.1.16
MIT + file LICENSE
Authors
Max Kuhn [aut, cre], Hadley Wickham [aut], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.