Convert Strings to Factors
step_string2factor
will convert one or more character
vectors to factors (ordered or unordered).
step_string2factor( recipe, ..., role = NA, trained = FALSE, levels = NULL, ordered = FALSE, skip = FALSE, id = rand_id("string2factor") ) ## S3 method for class 'step_string2factor' tidy(x, ...)
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variables will be converted to factors. 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 |
An options specification of the levels to be used
for the new factor. If left |
ordered |
A single logical value; should the factor(s) be ordered? |
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. |
x |
A |
If levels
is given, step_string2factor
will
convert all variables affected by this step to have the same
levels.
Also, note that prep
has an option strings_as_factors
that
defaults to TRUE
. This should be changed so that raw character
data will be applied to step_string2factor
. However, this step
can also take existing factors (but will leave them as-is).
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
selectors or variables selected) and ordered
.
library(modeldata) data(okc) rec <- recipe(~ diet + location, data = okc) make_factor <- rec %>% step_string2factor(diet) make_factor <- prep(make_factor, training = okc, strings_as_factors = FALSE) # note that `diet` is a factor bake(make_factor, new_data = NULL) %>% head okc %>% head tidy(make_factor, number = 1)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.