Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Dec 14, 2024
1 parent 0faf924 commit eccef12
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.13.0.18
Version: 0.13.0.19
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ CHANGES
* `data_summary()` also accepts the results of `bayestestR::ci()` as summary
function (#483).

* `ranktransform()` has a new argument `zeros` to determine how zeros should be
handled when `sign = TRUE` (#573).

BUG FIXES

* `describe_distribution()` no longer errors if the sample was too sparse to compute
Expand Down
35 changes: 28 additions & 7 deletions R/ranktransform.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' @param method Treatment of ties. Can be one of `"average"` (default),
#' `"first"`, `"last"`, `"random"`, `"max"` or `"min"`. See [rank()] for
#' details.
#' @param zeros How to handle zeros. If `"na"` (default), they are marked as
#' `NA`. If `"signrank"`, they are kept during the ranking and marked as zeros.
#' This is only used when `sign = TRUE`.
#' @param ... Arguments passed to or from other methods.
#' @inheritParams extract_column_names
#' @inheritParams standardize.data.frame
Expand All @@ -19,8 +22,11 @@
#' @examples
#' ranktransform(c(0, 1, 5, -5, -2))
#'
#' # Won't work
#' # ranktransform(c(0, 1, 5, -5, -2), sign = TRUE)
#' # By default, zeros are converted to NA
#' suppressWarnings(
#' ranktransform(c(0, 1, 5, -5, -2), sign = TRUE)
#' )
#' ranktransform(c(0, 1, 5, -5, -2), sign = TRUE, zeros = "signrank")
#'
#' head(ranktransform(trees))
#' @return A rank-transformed object.
Expand All @@ -38,13 +44,20 @@ ranktransform <- function(x, ...) {
ranktransform.numeric <- function(x,
sign = FALSE,
method = "average",
zeros = "na",
verbose = TRUE,
...) {
# no change if all values are `NA`s
if (all(is.na(x))) {
return(x)
}

zeros <- match.arg(zeros, c("na", "signrank"))
method <- match.arg(
method,
c("average", "first", "last", "random", "max", "min")
)

# Warning if only one value and return early
if (insight::has_single_value(x)) {
if (is.null(names(x))) {
Expand Down Expand Up @@ -74,12 +87,18 @@ ranktransform.numeric <- function(x,
}
}


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")
if (zeros == "na") {
out <- rep(NA, length(x))
ZEROES <- x == 0
if (any(ZEROES) && verbose) insight::format_warning("Zeros detected. These cannot be sign-rank transformed.")
out[!ZEROES] <- sign(x[!ZEROES]) * rank(abs(x[!ZEROES]),
ties.method = method,
na.last = "keep"
)
} else if (zeros == "signrank") {
out <- sign(x) * rank(abs(x), ties.method = method, na.last = "keep")
}
} else {
out <- rank(x, ties.method = method, na.last = "keep")
}
Expand All @@ -102,6 +121,7 @@ ranktransform.grouped_df <- function(x,
method = "average",
ignore_case = FALSE,
regex = FALSE,
zeros = "na",
verbose = TRUE,
...) {
info <- attributes(x)
Expand Down Expand Up @@ -143,6 +163,7 @@ ranktransform.data.frame <- function(x,
method = "average",
ignore_case = FALSE,
regex = FALSE,
zeros = "na",
verbose = TRUE,
...) {
# evaluate arguments
Expand Down
21 changes: 18 additions & 3 deletions man/ranktransform.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-ranktransform.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ test_that("signed rank works as expected", {
))))
})

test_that("argument 'zeros' works", {
x <- c(-1, 0, 2, -3, 4)
expect_warning(
ranktransform(x, sign = TRUE),
"cannot be sign-rank"
)
expect_identical(
ranktransform(x, sign = TRUE, zeros = "signrank"),
c(-2, 0, 3, -4, 5)
)
expect_error(
ranktransform(x, sign = TRUE, zeros = "foo"),
"should be one of"
)
})

test_that("ranktransform works with data frames", {
set.seed(123)
expect_snapshot(ranktransform(BOD))
Expand Down

0 comments on commit eccef12

Please sign in to comment.