From f34e7842c63cd753b0c2b67d11a6f426a742aa30 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 18 Dec 2023 12:40:16 +0100 Subject: [PATCH] lintr --- R/data_reverse.R | 30 +++++++++++++++--------------- R/utils_labels.R | 14 +++++++------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/R/data_reverse.R b/R/data_reverse.R index 6e1ef414b..2fc9ef493 100644 --- a/R/data_reverse.R +++ b/R/data_reverse.R @@ -93,21 +93,21 @@ reverse.numeric <- function(x, } # old minimum and maximum - min <- min(range) - max <- max(range) + 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, max) + sprintf("Did you want to provide `range = c(%g, %g)`?", min_value, max_value) ) } - new_min <- max - new_max <- min + new_min <- max_value + new_max <- min_value - out <- as.vector((new_max - new_min) / (max - min) * (x - min) + new_min) + 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) @@ -134,7 +134,9 @@ reverse.factor <- function(x, range = NULL, verbose = TRUE, ...) { # save for later use original_x <- x - if (!is.null(range)) { + 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.") @@ -180,8 +182,6 @@ reverse.factor <- function(x, range = NULL, verbose = TRUE, ...) { } old_levels <- range x <- factor(x, levels = range) - } else { - old_levels <- levels(x) } int_x <- as.integer(x) @@ -225,7 +225,7 @@ reverse.grouped_df <- function(x, # create the new variables and updates "select", so new variables are processed if (!isFALSE(append)) { # process arguments - args <- .process_append( + arguments <- .process_append( x, select, append, @@ -233,8 +233,8 @@ reverse.grouped_df <- function(x, preserve_value_labels = TRUE ) # update processed arguments - x <- args$x - select <- args$select + x <- arguments$x + select <- arguments$select } x <- as.data.frame(x) @@ -279,7 +279,7 @@ reverse.data.frame <- function(x, # create the new variables and updates "select", so new variables are processed if (!isFALSE(append)) { # process arguments - args <- .process_append( + arguments <- .process_append( x, select, append, @@ -287,8 +287,8 @@ reverse.data.frame <- function(x, preserve_value_labels = TRUE ) # update processed arguments - x <- args$x - select <- args$select + x <- arguments$x + select <- arguments$select } # Transform the range so that it is a list now diff --git a/R/utils_labels.R b/R/utils_labels.R index 4b28ac778..64b517086 100644 --- a/R/utils_labels.R +++ b/R/utils_labels.R @@ -6,24 +6,24 @@ .set_back_labels <- function(new, old, include_values = TRUE, reverse_values = FALSE) { # labelled data? attr(new, "label") <- attr(old, "label", exact = TRUE) - labels <- attr(old, "labels", exact = TRUE) + value_labels <- attr(old, "labels", exact = TRUE) # "include_values" is used to preserve value labels - if (isTRUE(include_values) && !is.null(labels)) { + if (isTRUE(include_values) && !is.null(value_labels)) { if (reverse_values) { # reverse values? Used for "reverse_scale()" - attr(new, "labels") <- stats::setNames(rev(labels), names(labels)) + attr(new, "labels") <- stats::setNames(rev(value_labels), names(value_labels)) } else { # keep value oder? Used for "to_numeric()" if (is.numeric(new)) { - if (any(grepl("[^0-9]", labels))) { + if (any(grepl("[^0-9]", value_labels))) { # if we have any non-numeric characters, convert to numeric - attr(new, "labels") <- stats::setNames(as.numeric(as.factor(labels)), names(labels)) + attr(new, "labels") <- stats::setNames(as.numeric(as.factor(value_labels)), names(value_labels)) } else { # if we have numeric, or "numeric character" (like "1", "2", "3" etc.) - attr(new, "labels") <- stats::setNames(as.numeric(labels), names(labels)) + attr(new, "labels") <- stats::setNames(as.numeric(value_labels), names(value_labels)) } } else { - attr(new, "labels") <- stats::setNames(labels, names(labels)) + attr(new, "labels") <- stats::setNames(value_labels, names(value_labels)) } } } else if (isFALSE(include_values)) {