step_bin2factor | R Documentation |
step_bin2factor()
creates a specification of a recipe step that will
create a two-level factor from a single dummy variable.
step_bin2factor(
recipe,
...,
role = NA,
trained = FALSE,
levels = c("yes", "no"),
ref_first = TRUE,
columns = NULL,
skip = FALSE,
id = rand_id("bin2factor")
)
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 |
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 character string of the selected variable names. This field
is a placeholder and will be populated once |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
id |
A character string that is unique to this step to identify it. |
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).
An updated version of recipe
with the new step added to the
sequence of any existing operations.
When you tidy()
this step, a tibble is returned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
The underlying operation does not allow for case weights.
Other dummy variable and encoding steps:
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_unknown()
,
step_unorder()
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)
rec <- prep(rec, training = covers)
results <- bake(rec, new_data = covers)
table(results$rocks, results$more_rocks)
tidy(rec, number = 3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.