Skip to content

Commit 8997aef

Browse files
Merge pull request #274 from tidymodels/fix-bug-clean_levels
fix bug in step_clean_levels()
2 parents 60999a6 + f38c4de commit 8997aef

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

NEWS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

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

7-
* `step_textfeatures()` has been made faster and more robust. #265
7+
* `step_textfeatures()` has been made faster and more robust. (#265)
8+
9+
* Fixed bug in `step_clean_levels()` where it would produce NAs for character columns. (#274)
810

911
# textrecipes 1.0.6
1012

R/clean_levels.R

+8-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,14 @@ bake.step_clean_levels <- function(object, new_data, ...) {
131131
}
132132

133133
for (col_name in col_names) {
134-
new_data[[col_name]] <- dplyr::recode_factor(
135-
new_data[[col_name]], !!!object$clean[[col_name]]
136-
)
134+
if (is.factor(new_data[[col_name]])) {
135+
new_data[[col_name]] <- dplyr::recode_factor(
136+
new_data[[col_name]], !!!object$clean[[col_name]]
137+
)
138+
} else {
139+
new_data[[col_name]] <- janitor::make_clean_names(new_data[[col_name]])
140+
141+
}
137142
}
138143

139144
new_data

tests/testthat/test-clean_levels.R

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test_that("character input", {
2222
cleaned_te <- bake(cleaned, new_data = smith_te)
2323

2424
expect_equal(sum(grepl(" ", cleaned_tr$name)), 0)
25+
expect_equal(sum(is.na(cleaned_tr$name)), 0)
2526
expect_equal(sum(levels(cleaned_tr$name) %in% smith_tr$name), 0)
2627

2728
tidy_exp_tr <- tibble(

0 commit comments

Comments
 (0)