step_combine_stringdist() creates a specification of a recipe step that
will combine factor levels that have a low stringdist between them.
Usage
step_combine_stringdist(
recipe,
...,
role = NA,
trained = FALSE,
distance = NULL,
res = NULL,
columns = NULL,
skip = FALSE,
id = rand_id("combine_stringdist")
)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 which variables are affected by the step. See
selections()for more details. For thetidymethod, 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.
- distance
Integer, value to determine which strings should be combined with which. The value is being used inclusive, so
2will combine levels that have a string distance between them of 2 or lower.- res
A list denoting the way the labels should be collapses is stored here once this preprocessing step has be trained by
prep().- columns
A character string of variable names that will be populated (eventually) by the
termsargument.- skip
A logical. Should the step be skipped when the recipe is baked by
bake()? While all operations are baked whenprep()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 usingskip = TRUEas 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 existing steps (if any). For the
tidy method, a tibble with columns terms (the
columns that will be affected) and base.
Examples
library(recipes)
library(tibble)
data0 <- tibble(
x1 = c("a", "b", "d", "e", "sfgsfgsd", "hjhgfgjgr"),
x2 = c("ak", "b", "djj", "e", "hjhgfgjgr", "hjhgfgjgr")
)
rec <- recipe(~., data = data0) %>%
step_combine_stringdist(all_predictors(), distance = 1) %>%
prep()
rec %>%
bake(new_data = NULL)
#> # A tibble: 6 × 2
#> x1 x2
#> <chr> <chr>
#> 1 a ak
#> 2 a b
#> 3 a djj
#> 4 a b
#> 5 sfgsfgsd hjhgfgjgr
#> 6 hjhgfgjgr hjhgfgjgr
tidy(rec, 1)
#> # A tibble: 12 × 4
#> terms from to id
#> <chr> <chr> <chr> <chr>
#> 1 x1 a a combine_stringdist_SwlKL
#> 2 x1 b a combine_stringdist_SwlKL
#> 3 x1 d a combine_stringdist_SwlKL
#> 4 x1 e a combine_stringdist_SwlKL
#> 5 x1 sfgsfgsd sfgsfgsd combine_stringdist_SwlKL
#> 6 x1 hjhgfgjgr hjhgfgjgr combine_stringdist_SwlKL
#> 7 x2 ak ak combine_stringdist_SwlKL
#> 8 x2 b b combine_stringdist_SwlKL
#> 9 x2 e b combine_stringdist_SwlKL
#> 10 x2 djj djj combine_stringdist_SwlKL
#> 11 x2 hjhgfgjgr hjhgfgjgr combine_stringdist_SwlKL
#> 12 x2 hjhgfgjgr hjhgfgjgr combine_stringdist_SwlKL
rec <- recipe(~., data = data0) %>%
step_combine_stringdist(all_predictors(), distance = 2) %>%
prep()
rec %>%
bake(new_data = NULL)
#> # A tibble: 6 × 2
#> x1 x2
#> <chr> <chr>
#> 1 a ak
#> 2 a ak
#> 3 a djj
#> 4 a ak
#> 5 sfgsfgsd hjhgfgjgr
#> 6 hjhgfgjgr hjhgfgjgr
tidy(rec, 1)
#> # A tibble: 12 × 4
#> terms from to id
#> <chr> <chr> <chr> <chr>
#> 1 x1 a a combine_stringdist_UUEdL
#> 2 x1 b a combine_stringdist_UUEdL
#> 3 x1 d a combine_stringdist_UUEdL
#> 4 x1 e a combine_stringdist_UUEdL
#> 5 x1 sfgsfgsd sfgsfgsd combine_stringdist_UUEdL
#> 6 x1 hjhgfgjgr hjhgfgjgr combine_stringdist_UUEdL
#> 7 x2 ak ak combine_stringdist_UUEdL
#> 8 x2 b ak combine_stringdist_UUEdL
#> 9 x2 e ak combine_stringdist_UUEdL
#> 10 x2 djj djj combine_stringdist_UUEdL
#> 11 x2 hjhgfgjgr hjhgfgjgr combine_stringdist_UUEdL
#> 12 x2 hjhgfgjgr hjhgfgjgr combine_stringdist_UUEdL