0
0
R-programmingHow-ToBeginner · 3 min read

How to Use Wilcox Test in R: Syntax and Examples

In R, use the wilcox.test() function to perform the Wilcoxon signed-rank test or Wilcoxon rank-sum test. Provide two numeric vectors or one vector with a paired argument to compare medians without assuming normal distribution.
📐

Syntax

The wilcox.test() function has two main forms:

  • Two-sample test: wilcox.test(x, y, paired = FALSE) compares two independent samples.
  • Paired test: wilcox.test(x, y, paired = TRUE) compares two related samples.
  • One-sample test: wilcox.test(x, mu = value) tests if median of x equals value.

Arguments:

  • x, y: numeric vectors of data.
  • paired: logical, TRUE for paired samples.
  • mu: median value for one-sample test.
r
wilcox.test(x, y = NULL, paired = FALSE, mu = 0, alternative = "two.sided")
💻

Example

This example shows a Wilcoxon rank-sum test comparing two independent groups and a Wilcoxon signed-rank test for paired data.

r
group1 <- c(5, 7, 8, 6, 9)
group2 <- c(10, 12, 11, 13, 14)

# Wilcoxon rank-sum test (two independent samples)
wilcox.test(group1, group2)

# Paired sample data
before <- c(20, 22, 19, 24, 25)
after <- c(21, 23, 20, 26, 27)

# Wilcoxon signed-rank test (paired samples)
wilcox.test(before, after, paired = TRUE)
Output
Wilcoxon rank sum test with continuity correction data: group1 and group2 W = 0, p-value = 0.007937 alternative hypothesis: true location shift is not equal to 0 Wilcoxon signed rank test with continuity correction data: before and after V = 0, p-value = 0.0625 alternative hypothesis: true location shift is not equal to 0
⚠️

Common Pitfalls

Common mistakes when using wilcox.test() include:

  • Not setting paired = TRUE for paired data, which changes the test type.
  • Passing non-numeric data or vectors with missing values without handling them.
  • Misinterpreting the test as a test for means instead of medians.

Always check your data type and pairing before running the test.

r
## Wrong: paired data treated as independent
wilcox.test(before, after)

## Right: specify paired = TRUE
wilcox.test(before, after, paired = TRUE)
📊

Quick Reference

ArgumentDescription
xNumeric vector of data or first sample
yOptional numeric vector of second sample
pairedTRUE for paired samples, FALSE for independent
muMedian value for one-sample test (default 0)
alternative"two.sided", "greater", or "less" for test direction

Key Takeaways

Use wilcox.test() to compare medians without assuming normal distribution.
Set paired = TRUE for related samples to perform the signed-rank test.
Provide two numeric vectors for independent samples or one vector for one-sample tests.
Check data types and handle missing values before testing.
Interpret results as median differences, not means.