Skip to content

Commit

Permalink
Merge pull request #274 from tidymodels/fix-bug-clean_levels
Browse files Browse the repository at this point in the history
fix bug in step_clean_levels()
  • Loading branch information
EmilHvitfeldt authored Oct 30, 2024
2 parents 60999a6 + f38c4de commit 8997aef
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

* Calling `?tidy.step_*()` now sends you to the documentation for `step_*()` where the outcome is documented. (#261)

* `step_textfeatures()` has been made faster and more robust. #265
* `step_textfeatures()` has been made faster and more robust. (#265)

* Fixed bug in `step_clean_levels()` where it would produce NAs for character columns. (#274)

# textrecipes 1.0.6

Expand Down
11 changes: 8 additions & 3 deletions R/clean_levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ bake.step_clean_levels <- function(object, new_data, ...) {
}

for (col_name in col_names) {
new_data[[col_name]] <- dplyr::recode_factor(
new_data[[col_name]], !!!object$clean[[col_name]]
)
if (is.factor(new_data[[col_name]])) {
new_data[[col_name]] <- dplyr::recode_factor(
new_data[[col_name]], !!!object$clean[[col_name]]
)
} else {
new_data[[col_name]] <- janitor::make_clean_names(new_data[[col_name]])

}
}

new_data
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-clean_levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test_that("character input", {
cleaned_te <- bake(cleaned, new_data = smith_te)

expect_equal(sum(grepl(" ", cleaned_tr$name)), 0)
expect_equal(sum(is.na(cleaned_tr$name)), 0)
expect_equal(sum(levels(cleaned_tr$name) %in% smith_tr$name), 0)

tidy_exp_tr <- tibble(
Expand Down

0 comments on commit 8997aef

Please sign in to comment.