Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 18, 2023
1 parent aa03f84 commit f34e784
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
30 changes: 15 additions & 15 deletions R/data_reverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -225,16 +225,16 @@ 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,
append_suffix = "_r",
preserve_value_labels = TRUE
)
# update processed arguments
x <- args$x
select <- args$select
x <- arguments$x
select <- arguments$select
}

x <- as.data.frame(x)
Expand Down Expand Up @@ -279,16 +279,16 @@ 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,
append_suffix = "_r",
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
Expand Down
14 changes: 7 additions & 7 deletions R/utils_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit f34e784

Please sign in to comment.