Skip to content

Commit

Permalink
lintr (reduce complexity)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 4, 2024
1 parent 131481b commit 836c4fe
Showing 1 changed file with 56 additions and 44 deletions.
100 changes: 56 additions & 44 deletions R/categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,8 @@ categorize.numeric <- function(x,
labels = NULL,
verbose = TRUE,
...) {
# 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."
)
}
# sanity check
split <- .sanitize_split_arg(split, n_groups, range)

# handle aliases
if (identical(split, "equal_length")) split <- "length"
Expand Down Expand Up @@ -221,28 +201,7 @@ categorize.numeric <- function(x,
original_x[!is.na(original_x)] <- out

# turn into factor?
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
.original_x_to_factor(original_x, labels, out, verbose)
}


Expand Down Expand Up @@ -387,3 +346,56 @@ categorize.grouped_df <- function(x,
}
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."

Check warning on line 371 in R/categorize.R

View check run for this annotation

Codecov / codecov/patch

R/categorize.R#L370-L371

Added lines #L370 - L371 were not covered by tests
)
}

split
}


.original_x_to_factor <- function(original_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

Check warning on line 388 in R/categorize.R

View check run for this annotation

Codecov / codecov/patch

R/categorize.R#L384-L388

Added lines #L384 - L388 were not covered by tests
} else {
labels <- stats::aggregate(x, list(no_na_x), FUN = stats::median, na.rm = TRUE)$x

Check warning on line 390 in R/categorize.R

View check run for this annotation

Codecov / codecov/patch

R/categorize.R#L390

Added line #L390 was not covered by tests
}
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."

Check warning on line 396 in R/categorize.R

View check run for this annotation

Codecov / codecov/patch

R/categorize.R#L392-L396

Added lines #L392 - L396 were not covered by tests
)
}
}
original_x
}

0 comments on commit 836c4fe

Please sign in to comment.