ranksum: Wilcoxon Rank-sum Test

View source: R/nonparametric.R View source: R/zzz-r4vn-inference.R

ranksumR Documentation

Wilcoxon Rank-sum Test

Description

Performs the Wilcoxon rank-sum test, also known as the Mann-Whitney U test, for two independent samples. Samples may be supplied as two numeric variables or as one numeric outcome and a two-level grouping variable.

Usage

ranksum(
  x,
  y = NULL,
  by = NULL,
  data = NULL,
  alternative = c("two.sided", "less", "greater"),
  exact = NULL,
  correct = TRUE,
  conf.int = TRUE,
  level = 0.95,
  digits = 3,
  p_digits = 3,
  show = TRUE,
  console = FALSE
)

Arguments

x

Numeric outcome or first numeric sample.

y

Optional second numeric sample.

by

Optional two-level grouping variable. Use either y or by.

data

Data frame. If NULL, the active R4VN data frame is used.

alternative

Alternative hypothesis: "two.sided", "less", or "greater".

exact

Use an exact p-value when possible. NULL lets R decide.

correct

Apply continuity correction for the normal approximation.

conf.int

Report the Hodges-Lehmann location-shift estimate and its confidence interval when available.

level

Confidence level.

digits, p_digits

Decimal places for estimates and p-values.

show

Logical; open the formatted result in the Viewer. Default TRUE.

console

Logical; also print the traditional result in the Console. Default FALSE.

Details

With by, the first observed factor level is sample 1 and the second level is sample 2. Use factor() or labvar(..., ref = ...) to control level order. Missing values are removed independently when x and y are supplied, and complete cases are used when by is supplied.

Value

Invisibly returns an object of class r4vn_stat.

Examples

d <- data.frame(
  score = c(12, 15, 11, 19, 18, 21, 14, 17),
  group = factor(rep(c("Control", "Intervention"), each = 4)),
  score2 = c(10, 13, 12, 14, 19, 20, 18, 22)
)
ranksum(score, by = group, data = d)
ranksum(score, score2, data = d)
ranksum(score, by = group, data = d, alternative = "less")

# Extended usage examples

d <- data.frame(
  score = c(10, 11, 12, 13, 18, 19, 20, 21),
  score2 = c(9, 10, 12, 11, 17, 18, 19, 22),
  group = factor(rep(c("Control", "Intervention"), each = 4))
)

# Two independent variables or one outcome by a two-level group
ranksum(score, score2, data = d)
ranksum(score, by = group, data = d)

# One-sided alternatives, approximation controls, and confidence interval
ranksum(score, by = group, data = d, alternative = "less")
ranksum(score, by = group, data = d, exact = FALSE, correct = FALSE)
ranksum(score, by = group, data = d, conf.int = FALSE)

# Active data and hidden console output
usedf(d)
result <- ranksum(score, by = group, show = FALSE)
result$raw$test


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.