Skip to content

step_factor2string will convert one or more factor vectors to strings.

Usage

step_factor2string(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  columns = FALSE,
  skip = FALSE,
  id = rand_id("factor2string")
)

Arguments

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 selections() for more details.

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.

columns

A character string of variables that will be converted. This is NULL until computed by prep().

skip

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 = TRUE as it may affect the computations for subsequent operations.

id

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

Value

An updated version of recipe with the new step added to the sequence of any existing operations.

Details

prep has an option strings_as_factors that defaults to TRUE. If this step is used with the default option, the string(s() produced by this step will be converted to factors after all of the steps have been prepped.

Tidying

When you tidy() this step, a tibble with columns terms (the columns that will be affected) is returned.

Case weights

The underlying operation does not allow for case weights.

Examples

data(Sacramento, package = "modeldata")

rec <- recipe(~ city + zip, data = Sacramento)

make_string <- rec %>%
  step_factor2string(city)

make_string <- prep(make_string,
  training = Sacramento,
  strings_as_factors = FALSE
)

make_string
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>  predictor          2
#> 
#> Training data contained 932 data points and no missing data.
#> 
#> Operations:
#> 
#> Character variables from city [trained]

# note that `city` is a string in recipe output
bake(make_string, new_data = NULL) %>% head()
#> # A tibble: 6 × 2
#>   city       zip   
#>   <chr>      <fct> 
#> 1 SACRAMENTO z95838
#> 2 SACRAMENTO z95823
#> 3 SACRAMENTO z95815
#> 4 SACRAMENTO z95815
#> 5 SACRAMENTO z95824
#> 6 SACRAMENTO z95841

# ...but remains a factor in the original data
Sacramento %>% head()
#> # A tibble: 6 × 9
#>   city       zip     beds baths  sqft type        price latitude longitude
#>   <fct>      <fct>  <int> <dbl> <int> <fct>       <int>    <dbl>     <dbl>
#> 1 SACRAMENTO z95838     2     1   836 Residential 59222     38.6     -121.
#> 2 SACRAMENTO z95823     3     1  1167 Residential 68212     38.5     -121.
#> 3 SACRAMENTO z95815     2     1   796 Residential 68880     38.6     -121.
#> 4 SACRAMENTO z95815     2     1   852 Residential 69307     38.6     -121.
#> 5 SACRAMENTO z95824     2     1   797 Residential 81900     38.5     -121.
#> 6 SACRAMENTO z95841     3     1  1122 Condo       89921     38.7     -121.