/
categorize.R
401 lines (366 loc) · 13.5 KB
/
categorize.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#' @title Recode (or "cut" / "bin") data into groups of values.
#' @name categorize
#'
#' @description
#' This functions divides the range of variables into intervals and recodes
#' the values inside these intervals according to their related interval.
#' It is basically a wrapper around base R's `cut()`, providing a simplified
#' and more accessible way to define the interval breaks (cut-off values).
#'
#' @param x A (grouped) data frame, numeric vector or factor.
#' @param split Character vector, indicating at which breaks to split variables,
#' or numeric values with values indicating breaks. If character, may be one
#' of `"median"`, `"mean"`, `"quantile"`, `"equal_length"`, or `"equal_range"`.
#' `"median"` or `"mean"` will return dichotomous variables, split at their
#' mean or median, respectively. `"quantile"` and `"equal_length"` will split
#' the variable into `n_groups` groups, where each group refers to an interval
#' of a specific range of values. Thus, the length of each interval will be
#' based on the number of groups. `"equal_range"` also splits the variable
#' into multiple groups, however, the length of the interval is given, and
#' the number of resulting groups (and hence, the number of breaks) will be
#' determined by how many intervals can be generated, based on the full range
#' of the variable.
#' @param n_groups If `split` is `"quantile"` or `"equal_length"`, this defines
#' the number of requested groups (i.e. resulting number of levels or values)
#' for the recoded variable(s). `"quantile"` will define intervals based
#' on the distribution of the variable, while `"equal_length"` tries to
#' divide the range of the variable into pieces of equal length.
#' @param range If `split = "equal_range"`, this defines the range of values
#' that are recoded into a new value.
#' @param lowest Minimum value of the recoded variable(s). If `NULL` (the default),
#' for numeric variables, the minimum of the original input is preserved. For
#' factors, the default minimum is `1`. For `split = "equal_range"`, the
#' default minimum is always `1`, unless specified otherwise in `lowest`.
#' @param labels Character vector of value labels. If not `NULL`, `categorize()`
#' will returns factors instead of numeric variables, with `labels` used
#' for labelling the factor levels. Can also be `"mean"` or `"median"` for a
#' factor with labels as the mean/median of each groups.
#' @param append Logical or string. If `TRUE`, recoded or converted variables
#' get new column names and are appended (column bind) to `x`, thus returning
#' both the original and the recoded variables. The new columns get a suffix,
#' based on the calling function: `"_r"` for recode functions, `"_n"` for
#' `to_numeric()`, `"_f"` for `to_factor()`, or `"_s"` for
#' `slide()`. If `append=FALSE`, original variables in `x` will be
#' overwritten by their recoded versions. If a character value, recoded
#' variables are appended with new column names (using the defined suffix) to
#' the original data frame.
#' @param ... not used.
#' @inheritParams find_columns
#'
#' @inherit data_rename seealso
#'
#' @details
#'
#' # Splits and breaks (cut-off values)
#'
#' Breaks are in general _exclusive_, this means that these values indicate
#' the lower bound of the next group or interval to begin. Take a simple
#' example, a numeric variable with values from 1 to 9. The median would be 5,
#' thus the first interval ranges from 1-4 and is recoded into 1, while 5-9
#' would turn into 2 (compare `cbind(1:9, categorize(1:9))`). The same variable,
#' using `split = "quantile"` and `n_groups = 3` would define breaks at 3.67
#' and 6.33 (see `quantile(1:9, probs = c(1/3, 2/3))`), which means that values
#' from 1 to 3 belong to the first interval and are recoded into 1 (because
#' the next interval starts at 3.67), 4 to 6 into 2 and 7 to 9 into 3.
#'
#' # Recoding into groups with equal size or range
#'
#' `split = "equal_length"` and `split = "equal_range"` try to divide the
#' range of `x` into intervals of similar (or same) length. The difference is
#' that `split = "equal_length"` will divide the range of `x` into `n_groups`
#' pieces and thereby defining the intervals used as breaks (hence, it is
#' equivalent to `cut(x, breaks = n_groups)`), while `split = "equal_range"`
#' will cut `x` into intervals that all have the length of `range`, where the
#' first interval by defaults starts at `1`. The lowest (or starting) value
#' of that interval can be defined using the `lowest` argument.
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @return `x`, recoded into groups. By default `x` is numeric, unless `labels`
#' is specified. In this case, a factor is returned, where the factor levels
#' (i.e. recoded groups are labelled accordingly.
#'
#' @examples
#' set.seed(123)
#' x <- sample(1:10, size = 50, replace = TRUE)
#'
#' table(x)
#'
#' # by default, at median
#' table(categorize(x))
#'
#' # into 3 groups, based on distribution (quantiles)
#' table(categorize(x, split = "quantile", n_groups = 3))
#'
#' # into 3 groups, user-defined break
#' table(categorize(x, split = c(3, 5)))
#'
#' set.seed(123)
#' x <- sample(1:100, size = 500, replace = TRUE)
#'
#' # into 5 groups, try to recode into intervals of similar length,
#' # i.e. the range within groups is the same for all groups
#' table(categorize(x, split = "equal_length", n_groups = 5))
#'
#' # into 5 groups, try to return same range within groups
#' # i.e. 1-20, 21-40, 41-60, etc. Since the range of "x" is
#' # 1-100, and we have a range of 20, this results into 5
#' # groups, and thus is for this particular case identical
#' # to the previous result.
#' table(categorize(x, split = "equal_range", range = 20))
#'
#' # return factor with value labels instead of numeric value
#' set.seed(123)
#' x <- sample(1:10, size = 30, replace = TRUE)
#' categorize(x, "equal_length", n_groups = 3)
#' categorize(x, "equal_length", n_groups = 3, labels = c("low", "mid", "high"))
#'
#' # cut numeric into groups with the mean or median as a label name
#' x <- sample(1:10, size = 30, replace = TRUE)
#' categorize(x, "equal_length", n_groups = 3, labels = "mean")
#' categorize(x, "equal_length", n_groups = 3, labels = "median")
#' @export
categorize <- function(x, ...) {
UseMethod("categorize")
}
#' @export
categorize.default <- function(x, verbose = TRUE, ...) {
if (isTRUE(verbose)) {
insight::format_alert(
paste0("Variables of class `", class(x)[1], "` can't be recoded and remain unchanged.")
)
}
return(x)
}
#' @rdname categorize
#' @export
categorize.numeric <- function(x,
split = "median",
n_groups = NULL,
range = NULL,
lowest = 1,
labels = NULL,
verbose = TRUE,
...) {
# sanity check
split <- .sanitize_split_arg(split, n_groups, range)
# handle aliases
if (identical(split, "equal_length")) split <- "length"
if (identical(split, "equal_range")) split <- "range"
# save
original_x <- x
# no missings
x <- stats::na.omit(x)
# stop if all NA
if (!length(x)) {
if (isTRUE(verbose)) {
insight::format_alert(
"Variable contains only missing values. No recoding carried out."
)
}
return(original_x)
}
if (is.numeric(split)) {
breaks <- split
} else {
breaks <- switch(split,
median = stats::median(x),
mean = mean(x),
length = n_groups,
quantile = stats::quantile(x, probs = seq_len(n_groups) / n_groups),
range = .equal_range(x, range, n_groups, lowest),
NULL
)
}
# complete ranges, including minimum and maximum
if (!identical(split, "length")) breaks <- unique(c(min(x), breaks, max(x)))
# recode into groups
out <- droplevels(cut(
x,
breaks = breaks,
include.lowest = TRUE,
right = FALSE
))
levels(out) <- 1:nlevels(out)
# fix lowest value, add back into original vector
out <- as.numeric(out)
if (!is.null(lowest)) {
out <- out - (min(out) - lowest)
}
original_x[!is.na(original_x)] <- out
# turn into factor?
.original_x_to_factor(original_x, x, labels, out, verbose, ...)
}
#' @export
categorize.factor <- function(x, ...) {
original_x <- x
levels(x) <- 1:nlevels(x)
out <- as.factor(categorize(as.numeric(x), ...))
.set_back_labels(out, original_x, include_values = FALSE)
}
#' @rdname categorize
#' @export
categorize.data.frame <- function(x,
select = NULL,
exclude = NULL,
split = "median",
n_groups = NULL,
range = NULL,
lowest = 1,
labels = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
verbose = verbose
)
# when we append variables, we call ".process_append()", which will
# create the new variables and updates "select", so new variables are processed
if (!isFALSE(append)) {
# process arguments
my_args <- .process_append(
x,
select,
append,
append_suffix = "_r"
)
# update processed arguments
x <- my_args$x
select <- my_args$select
}
x[select] <- lapply(
x[select],
categorize,
split = split,
n_groups = n_groups,
range = range,
lowest = lowest,
labels = labels,
verbose = verbose,
...
)
x
}
#' @export
categorize.grouped_df <- function(x,
select = NULL,
exclude = NULL,
split = "median",
n_groups = NULL,
range = NULL,
lowest = 1,
labels = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
# works only for dplyr >= 0.8.0
grps <- attr(x, "groups", exact = TRUE)[[".rows"]]
attr_data <- attributes(x)
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
remove_group_var = TRUE,
verbose = verbose
)
# when we append variables, we call ".process_append()", which will
# create the new variables and updates "select", so new variables are processed
if (!isFALSE(append)) {
# process arguments
my_args <- .process_append(
x,
select,
append,
append_suffix = "_r"
)
# update processed arguments
x <- my_args$x
select <- my_args$select
}
x <- as.data.frame(x)
for (rows in grps) {
x[rows, ] <- categorize(
x[rows, , drop = FALSE],
split = split,
n_groups = n_groups,
range = range,
lowest = lowest,
labels = labels,
select = select,
exclude = exclude,
append = FALSE, # need to set to FALSE here, else variable will be doubled
ignore_case = ignore_case,
verbose = verbose,
...
)
}
# set back class, so data frame still works with dplyr
x <- .replace_attrs(x, attr_data)
x
}
# tools --------------------
.equal_range <- function(x, range, n_groups, lowest = NULL) {
if (is.null(lowest)) lowest <- 1
if (is.null(range)) {
size <- ceiling((max(x) - min(x)) / n_groups)
range <- as.numeric(size)
}
seq(lowest, max(x), by = range)
}
.sanitize_split_arg <- function(split, n_groups, range) {
# check arguments
if (is.character(split)) {
split <- match.arg(
split,
choices = c(
"median", "mean", "quantile", "equal_length", "equal_range",
"equal", "equal_distance", "range", "distance"
)
)
}
if (is.character(split) && split %in% c("quantile", "equal_length") && is.null(n_groups)) {
insight::format_error(
"Recoding based on quantiles or equal-sized groups requires the `n_groups` argument to be specified."
)
}
if (is.character(split) && split == "equal_range" && is.null(n_groups) && is.null(range)) {
insight::format_error(
"Recoding into groups with equal range requires either the `range` or `n_groups` argument to be specified."
)
}
split
}
.original_x_to_factor <- function(original_x, x, labels, out, verbose, ...) {
if (!is.null(labels)) {
if (length(labels) == length(unique(out))) {
original_x <- as.factor(original_x)
levels(original_x) <- labels
} else if (length(labels) == 1 && labels %in% c("mean", "median")) {
original_x <- as.factor(original_x)
no_na_x <- original_x[!is.na(original_x)]
if (labels == "mean") {
labels <- stats::aggregate(x, list(no_na_x), FUN = mean, na.rm = TRUE)$x
} else {
labels <- stats::aggregate(x, list(no_na_x), FUN = stats::median, na.rm = TRUE)$x
}
levels(original_x) <- insight::format_value(labels, ...)
} else if (isTRUE(verbose)) {
insight::format_warning(
"Argument `labels` and levels of the recoded variable are not of the same length.",
"Variable will not be converted to factor."
)
}
}
original_x
}