/
clean_names.R
153 lines (142 loc) · 3.58 KB
/
clean_names.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#' Clean Variable Names
#'
#' `step_clean_names()` creates a *specification* of a recipe step that will
#' clean variable names so the names consist only of letters, numbers, and the
#' underscore.
#
#' @template args-recipe
#' @template args-dots
#' @template args-role_no-new
#' @template args-trained
#' @param clean A named character vector to clean variable names. This is `NULL`
#' until computed by [recipes::prep.recipe()].
#' @template args-skip
#' @template args-id
#'
#' @template returns
#'
#' @details
#'
#' # Tidying
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble is returned with
#' columns `terms`, `value`, and `id`:
#'
#' \describe{
#' \item{terms}{character, the new clean variable names}
#' \item{value}{character, the original variable names}
#' \item{id}{character, id of this step}
#' }
#'
#' @template case-weights-not-supported
#'
#' @seealso [step_clean_levels()], [recipes::step_factor2string()],
#' [recipes::step_string2factor()], [recipes::step_regex()],
#' [recipes::step_unknown()], [recipes::step_novel()], [recipes::step_other()]
#' @family Steps for Text Cleaning
#'
#' @examplesIf rlang::is_installed("janitor")
#' library(recipes)
#' data(airquality)
#'
#' air_tr <- tibble(airquality[1:100, ])
#' air_te <- tibble(airquality[101:153, ])
#'
#' rec <- recipe(~., data = air_tr)
#'
#' rec <- rec %>%
#' step_clean_names(all_predictors())
#' rec <- prep(rec, training = air_tr)
#' tidy(rec, number = 1)
#'
#' bake(rec, air_tr)
#' bake(rec, air_te)
#' @export
step_clean_names <-
function(recipe,
...,
role = NA,
trained = FALSE,
clean = NULL,
skip = FALSE,
id = rand_id("clean_names")) {
add_step(
recipe,
step_clean_names_new(
terms = enquos(...),
role = role,
trained = trained,
clean = clean,
skip = skip,
id = id
)
)
}
step_clean_names_new <-
function(terms, role, trained, clean, skip, id) {
step(
subclass = "clean_names",
terms = terms,
role = role,
trained = trained,
clean = clean,
skip = skip,
id = id
)
}
#' @export
prep.step_clean_names <- function(x, training, info = NULL, ...) {
col_names <- recipes_eval_select(x$terms, training, info)
if (length(col_names) > 0) {
cleaned <- janitor::make_clean_names(col_names)
clean <- rlang::set_names(cleaned, col_names)
} else {
clean <- NULL
}
step_clean_names_new(
terms = x$terms,
role = x$role,
trained = TRUE,
clean = clean,
skip = x$skip,
id = x$id
)
}
#' @export
bake.step_clean_names <- function(object, new_data, ...) {
col_names <- object$clean
check_new_data(names(col_names), object, new_data)
if (!is.null(col_names)) {
colnames(new_data) <- dplyr::recode(colnames(new_data), !!!col_names)
}
new_data
}
#' @export
print.step_clean_names <-
function(x, width = max(20, options()$width - 30), ...) {
title <- "Cleaning variable names for "
print_step(names(x$clean), x$terms, x$trained, title, width)
invisible(x)
}
#' @rdname step_clean_names
#' @usage NULL
#' @export
tidy.step_clean_names <- function(x, ...) {
if (is_trained(x)) {
if (is.null(x$clean)) {
res <- tibble(terms = character())
} else {
res <- tibble::tibble(terms = unname(x$clean), value = names(x$clean))
}
} else {
term_names <- sel2char(x$terms)
res <- tibble(terms = term_names)
}
res$id <- x$id
res
}
#' @rdname required_pkgs.step
#' @export
required_pkgs.step_clean_names <- function(x, ...) {
c("textrecipes", "janitor")
}