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
|
#' (Signed) rank transformation
#'
#' Transform numeric values with the integers of their rank (i.e., 1st smallest,
#' 2nd smallest, 3rd smallest, etc.). Setting the `sign` argument to `TRUE` will
#' give you signed ranks, where the ranking is done according to absolute size
#' but where the sign is preserved (i.e., 2, 1, -3, 4).
#'
#' @param x Object.
#' @param sign Logical, if `TRUE`, return signed ranks.
#' @param method Treatment of ties. Can be one of `"average"` (default),
#' `"first"`, `"last"`, `"random"`, `"max"` or `"min"`. See [rank()] for
#' details.
#' @param ... Arguments passed to or from other methods.
#' @inheritParams find_columns
#' @inheritParams standardize.data.frame
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @examples
#' ranktransform(c(0, 1, 5, -5, -2))
#' ranktransform(c(0, 1, 5, -5, -2), sign = TRUE)
#'
#' head(ranktransform(trees))
#' @return A rank-transformed object.
#'
#' @family transform utilities
#'
#' @export
ranktransform <- function(x, ...) {
UseMethod("ranktransform")
}
#' @rdname ranktransform
#' @export
ranktransform.numeric <- function(x,
sign = FALSE,
method = "average",
verbose = TRUE,
...) {
# no change if all values are `NA`s
if (all(is.na(x))) {
return(x)
}
# Warning if only one value and return early
if (insight::has_single_value(x)) {
if (is.null(names(x))) {
name <- deparse(substitute(x))
} else {
name <- names(x)
}
if (verbose) {
insight::format_warning(paste0("Variable `", name, "` contains only one unique value and will not be normalized."))
}
return(x)
}
# Warning if only two values present but don't return early
if (length(unique(x)) == 2L) {
if (is.null(names(x))) {
name <- deparse(substitute(x))
} else {
name <- names(x)
}
if (verbose) {
insight::format_warning(paste0("Variable `", name, "` contains only two different values. Consider converting it to a factor."))
}
}
if (sign) {
ZEROES <- x == 0
if (any(ZEROES) && verbose) insight::format_warning("Zeros detected. These cannot be sign-rank transformed.")
out <- rep(NA, length(x))
out[!ZEROES] <- sign(x[!ZEROES]) * rank(abs(x[!ZEROES]), ties.method = method, na.last = "keep")
} else {
out <- rank(x, ties.method = method, na.last = "keep")
}
out
}
#' @export
ranktransform.factor <- function(x, ...) {
x
}
#' @export
ranktransform.grouped_df <- function(x,
select = NULL,
exclude = NULL,
sign = FALSE,
method = "average",
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
info <- attributes(x)
# works only for dplyr >= 0.8.0
grps <- attr(x, "groups", exact = TRUE)[[".rows"]]
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
verbose = verbose
)
x <- as.data.frame(x)
for (rows in grps) {
x[rows, ] <- ranktransform(
x[rows, , drop = FALSE],
select = select,
exclude = exclude,
sign = sign,
method = method,
...
)
}
# set back class, so data frame still works with dplyr
attributes(x) <- info
x
}
#' @rdname ranktransform
#' @export
ranktransform.data.frame <- function(x,
select = NULL,
exclude = NULL,
sign = FALSE,
method = "average",
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
verbose = verbose
)
x[select] <- lapply(x[select], ranktransform, sign = sign, method = method)
x
}
|