1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/string2factor.R
3\name{step_string2factor}
4\alias{step_string2factor}
5\title{Convert Strings to Factors}
6\usage{
7step_string2factor(
8  recipe,
9  ...,
10  role = NA,
11  trained = FALSE,
12  levels = NULL,
13  ordered = FALSE,
14  skip = FALSE,
15  id = rand_id("string2factor")
16)
17}
18\arguments{
19\item{recipe}{A recipe object. The step will be added to the
20sequence of operations for this recipe.}
21
22\item{...}{One or more selector functions to choose variables
23for this step. See \code{\link[=selections]{selections()}} for more details.}
24
25\item{role}{Not used by this step since no new variables are
26created.}
27
28\item{trained}{A logical to indicate if the quantities for
29preprocessing have been estimated.}
30
31\item{levels}{An options specification of the levels to be used
32for the new factor. If left \code{NULL}, the sorted unique
33values present when \code{bake} is called will be used.}
34
35\item{ordered}{A single logical value; should the factor(s) be
36ordered?}
37
38\item{skip}{A logical. Should the step be skipped when the
39recipe is baked by \code{\link[=bake.recipe]{bake.recipe()}}? While all operations are baked
40when \code{\link[=prep.recipe]{prep.recipe()}} is run, some operations may not be able to be
41conducted on new data (e.g. processing the outcome variable(s)).
42Care should be taken when using \code{skip = TRUE} as it may affect
43the computations for subsequent operations.}
44
45\item{id}{A character string that is unique to this step to identify it.}
46}
47\value{
48An updated version of \code{recipe} with the new step added to the
49sequence of any existing operations.
50}
51\description{
52\code{step_string2factor} will convert one or more character
53vectors to factors (ordered or unordered).
54}
55\details{
56If \code{levels} is given, \code{step_string2factor} will
57convert all variables affected by this step to have the same
58levels.
59
60Also, note that \code{prep} has an option \code{strings_as_factors} that
61defaults to \code{TRUE}. This should be changed so that raw character
62data will be applied to \code{step_string2factor}. However, this step
63can also take existing factors (but will leave them as-is).
64
65When you \code{\link[=tidy]{tidy()}} this step, a tibble with columns \code{terms} (the
66selectors or variables selected) and \code{ordered} is returned.
67}
68\examples{
69library(modeldata)
70data(okc)
71
72rec <- recipe(~ diet + location, data = okc)
73
74make_factor <- rec \%>\%
75  step_string2factor(diet)
76make_factor <- prep(make_factor,
77                    training = okc,
78                    strings_as_factors = FALSE)
79
80# note that `diet` is a factor
81bake(make_factor, new_data = NULL) \%>\% head
82okc \%>\% head
83tidy(make_factor, number = 1)
84}
85\seealso{
86Other dummy variable and encoding steps:
87\code{\link{step_bin2factor}()},
88\code{\link{step_count}()},
89\code{\link{step_date}()},
90\code{\link{step_dummy_multi_choice}()},
91\code{\link{step_dummy}()},
92\code{\link{step_factor2string}()},
93\code{\link{step_holiday}()},
94\code{\link{step_indicate_na}()},
95\code{\link{step_integer}()},
96\code{\link{step_novel}()},
97\code{\link{step_num2factor}()},
98\code{\link{step_ordinalscore}()},
99\code{\link{step_other}()},
100\code{\link{step_regex}()},
101\code{\link{step_relevel}()},
102\code{\link{step_unknown}()},
103\code{\link{step_unorder}()}
104}
105\concept{dummy variable and encoding steps}
106