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
|
#' Reverse-Score Variables
#'
#' Reverse-score variables (change the keying/scoring direction).
#'
#' @param range Range of values that is used as reference for reversing the
#' scale. For numeric variables, can be `NULL` or a numeric vector of length
#' two, indicating the lowest and highest value of the reference range. If
#' `NULL`, will take the range of the input vector (`range(x)`). For factors,
#' `range` can be `NULL`, a numeric vector of length two, or a (numeric)
#' vector of at least the same length as factor levels (i.e. must be equal
#' to or larger than `nlevels(x)`). Note that providing a `range` for factors
#' usually only makes sense when factor levels are numeric, not characters.
#' @param ... Arguments passed to or from other methods.
#' @inheritParams categorize
#' @inheritParams extract_column_names
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @examples
#' reverse(c(1, 2, 3, 4, 5))
#' reverse(c(-2, -1, 0, 2, 1))
#'
#' # Specify the "theoretical" range of the input vector
#' reverse(c(1, 3, 4), range = c(0, 4))
#'
#' # Factor variables
#' reverse(factor(c(1, 2, 3, 4, 5)))
#' reverse(factor(c(1, 2, 3, 4, 5)), range = 0:10)
#'
#' # Data frames
#' head(reverse(iris))
#' head(reverse(iris, select = "Sepal.Length"))
#'
#' @return A reverse-scored object.
#'
#' @family transform utilities
#'
#' @inherit data_rename seealso
#'
#' @export
reverse <- function(x, ...) {
UseMethod("reverse")
}
#' @rdname reverse
#' @export
reverse_scale <- reverse
#' @export
reverse.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."
)
)
}
x
}
#' @rdname reverse
#' @export
reverse.numeric <- function(x,
range = NULL,
verbose = TRUE,
...) {
# Warning if all NaNs
if (all(is.na(x))) {
return(x)
}
# Warning if only one value
if (insight::has_single_value(x) && is.null(range)) {
if (verbose) {
insight::format_warning("A `range` must be provided for data with only one unique value.")
}
return(x)
}
# no missing values allowed
if (anyNA(range)) {
insight::format_error("`range` is not allowed to have missing values.")
}
if (is.null(range)) {
range <- c(min(x, na.rm = TRUE), max(x, na.rm = TRUE))
}
# old minimum and maximum
min_value <- min(range)
max_value <- max(range)
# check if a valid range (i.e. vector of length 2) is provided
if (length(range) > 2) {
insight::format_error(
"`range` must be a numeric vector of length two, indicating lowest and highest value of the required range.",
sprintf("Did you want to provide `range = c(%g, %g)`?", min_value, max_value)
)
}
new_min <- max_value
new_max <- min_value
out <- as.vector((new_max - new_min) / (max_value - min_value) * (x - min_value) + new_min)
# labelled data?
out <- .set_back_labels(out, x, reverse_values = TRUE)
out
}
#' @export
reverse.factor <- function(x, range = NULL, verbose = TRUE, ...) {
# Warning if all NaNs
if (all(is.na(x))) {
return(x)
}
# Warning if only one value
if (insight::has_single_value(x) && is.null(range)) {
if (verbose) {
insight::format_warning("A `range` must be provided for data with only one unique value.")
}
return(x)
}
# save for later use
original_x <- x
if (is.null(range)) {
old_levels <- levels(x)
} else {
# no missing values allowed
if (anyNA(range)) {
insight::format_error("`range` is not allowed to have missing values.")
}
range_ok <- TRUE
# if we have a vector of length 2 for range, and more factor levels,
# we assume `range` indicates minimum and maximum of range values
if (length(range) == 2 && nlevels(droplevels(x)) > 2) {
if (is.numeric(range)) {
range <- min(range):max(range)
} else {
# if range is of length 2, and we have more than 2 number of levels,
# range must be numeric to indicate minima and maxima. if not, stop.
range_ok <- FALSE
}
}
if (length(range) > 2 && length(range) < nlevels(droplevels(x))) {
# if range has more than two values, but fewer values than number of
# factor levels, we cannot associate the reversed scale, so stop
range_ok <- FALSE
}
if (!range_ok) {
insight::format_error(
"`range` must be one of the following:",
"- a numeric vector of length two, indicating lowest and highest value of the required range,",
"- a vector (numeric or character) of values with at least as many values as number of levels in `x`,",
"- or `NULL`."
)
}
# check if no or not all old levels are in new range
if (verbose) {
if (!any(levels(x) %in% as.character(range))) {
insight::format_warning(
"No current factor level is included in `range`.",
"Returned factor will only contain missing values."
)
} else if (!all(levels(x) %in% as.character(range))) {
insight::format_warning(
"Not all current factor levels are included in `range`.",
"Returned factor will contain missing values."
)
}
}
old_levels <- range
x <- factor(x, levels = range)
}
int_x <- as.integer(x)
rev_x <- reverse(int_x, range = c(1, length(old_levels)))
x <- factor(rev_x, levels = seq_len(length(old_levels)), labels = old_levels)
# labelled data?
x <- .set_back_labels(x, original_x, reverse_values = TRUE)
x
}
#' @export
reverse.grouped_df <- function(x,
select = NULL,
exclude = NULL,
range = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = FALSE,
...) {
info <- attributes(x)
grps <- attr(x, "groups", exact = TRUE)[[".rows"]]
# 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
arguments <- .process_append(
x,
select,
append,
append_suffix = "_r",
preserve_value_labels = TRUE
)
# update processed arguments
x <- arguments$x
select <- arguments$select
}
x <- as.data.frame(x)
for (rows in grps) {
x[rows, ] <- reverse(
x[rows, , drop = FALSE],
select = select,
exclude = exclude,
range = range,
append = FALSE, # need to set to FALSE here, else variable will be doubled
...
)
}
# set back class, so data frame still works with dplyr
attributes(x) <- utils::modifyList(info, attributes(x))
x
}
#' @rdname reverse
#' @export
reverse.data.frame <- function(x,
select = NULL,
exclude = NULL,
range = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = FALSE,
...) {
# 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
arguments <- .process_append(
x,
select,
append,
append_suffix = "_r",
preserve_value_labels = TRUE
)
# update processed arguments
x <- arguments$x
select <- arguments$select
}
# Transform the range so that it is a list now
if (!is.null(range) && !is.list(range)) {
range <- stats::setNames(rep(list(range), length(select)), select)
}
x[select] <- lapply(select, function(n) {
reverse(x[[n]], range = range[[n]])
})
x
}
|