Wilcoxon Signed Rank Test in R Programming
Last Updated :
15 Jul, 2025
The Wilcoxon signed rank test is a non-parametric method used to compare two related groups. It works well when we have matched data or repeated measurements from the same group and want to see if there is a meaningful difference between them. This test is often used as an alternative to the paired t-test, especially when the data does not follow a normal distribution.
In simple terms, the Wilcoxon test helps us find out if the differences between two sets of related data are likely due to chance or if they suggest a real change.
Types of Wilcoxon Signed-Rank Test in R
This test can be divided into two parts:
- One-Sample Wilcoxon Signed Rank Test
- Paired Samples Wilcoxon Test
1. One-Sample Wilcoxon Signed Rank Test
The one-sample Wilcoxon signed-rank test is a non-parametric alternative to a one-sample t-test , when the data cannot be assumed to be normally distributed. It’s used to determine whether the median of the sample is equal to a known standard value (theoretical value) .
Implementation in R
To perform a one-sample Wilcoxon test, R provides a function wilcox.test() that can be used as follow:
Syntax: wilcox.test(x, mu = 0, alternative = "two.sided")
Parameters:
- x: a numeric vector containing your data values
- mu: the theoretical mean/median value. Default is 0 but you can change it.
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
R
set.seed(1234)
myData <- data.frame(
name = paste0("R_", 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
print(myData)
result <- wilcox.test(myData$weight, mu = 25)
print(result)
Output:
name weight
1 R_1 27.6
2 R_2 30.6
3 R_3 32.2
4 R_4 25.3
5 R_5 30.9
6 R_6 31.0
7 R_7 28.9
8 R_8 28.9
9 R_9 28.9
10 R_10 28.2
Wilcoxon signed rank test with continuity correction
data: myData$weight
V = 55, p-value = 0.005793
alternative hypothesis: true location is not equal to 25
Since the p-value of the test is 0.005793, which is less than the significance level alpha = 0.05, we can reject the null hypothesis and conclude that the average weight of the rabbit is significantly different from 25g.
To test whether the median is less than 25g,
R
wilcox.test(myData$weight, mu = 25, alternative = "less")
Output:
Wilcoxon signed rank exact test
data: myData$weight
V = 55, p-value = 0.9979
alternative hypothesis: true location is less than 25
To test whether the median is greater than 25g,
R
wilcox.test(myData$weight, mu = 25, alternative = "greater")
Output:
Wilcoxon signed rank exact test
data: myData$weight
V = 55, p-value = 0.002897
alternative hypothesis: true location is less than 25
2. Paired Samples Wilcoxon Test in R
The paired samples Wilcoxon test is a non-parametric alternative to paired t-test used to compare paired data. It’s used when you have two related measurements for the same subjects, like before and after a treatment. It compares whether there is a significant shift in the median.
Implementation in R
To perform Paired Samples Wilcoxon-test, the R provides a function wilcox.test() that can be used as follow:
Syntax: wilcox.test(x, y, paired = TRUE, alternative = "two.sided")
Parameters:
- x, y: numeric vectors
- paired: a logical value specifying that we want to compute a paired Wilcoxon test
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
Example: Compare weights before and after treatment
R
before <- c(190.1, 190.9, 172.7, 213, 231.4,
196.9, 172.2, 285.5, 225.2, 113.7)
after <- c(392.9, 313.2, 345.1, 393, 434,
227.9, 422, 383.9, 392.3, 352.2)
result <- wilcox.test(before, after, paired = TRUE)
print(result)
Output:
Wilcoxon signed rank test
data: before and after
V = 0, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0
In the above output, the p-value of the test is 0.001953, which is less than the significance level alpha = 0.05. We can conclude that the median weight of the mice before treatment is significantly different from the median weight after treatment with a p-value = 0.001953.
To check if weight before treatment is less than after:
R
wilcox.test(before, after, paired = TRUE, alternative = "less")
Output:
Wilcoxon signed rank test
data: weight by group
V = 55, p-value = 0.0009766
alternative hypothesis: true location shift is less than 0
To check if weight before treatment is greater than after:
R
wilcox.test(before, after, paired = TRUE, alternative = "greater")
Output:
Wilcoxon signed rank test
data: weight by group
V = 55, p-value = 1
alternative hypothesis: true location shift is greater than 0
In this article, we performed the Wilcoxon Signed Rank Test in R for both one-sample and paired sample scenarios, along with practical examples and interpretation of results.
Wilcoxon Signed Rank Test in R Programming
Similar Reads
ShapiroâWilk Test in R Programming The Shapiro-Wilk's test or Shapiro test is a normality test in frequentist statistics. The null hypothesis of Shapiro's test is that the population is distributed normally. It is among the three tests for normality designed for detecting all kinds of departure from normality. If the value of p is eq
4 min read
How to Conduct a Wilcoxon Signed-Rank Test in Python? Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing In this article, we are going to see how to conduct a Wilcoxon signed-Rank test in the Python programming language. Wilcoxon signed-rank test, also known as Wilcoxon matched pair test is a non-parametric hypothesis test that c
3 min read
Mann Whitney U Test in R Programming A popular nonparametric(distribution-free) test to compare outcomes between two independent groups is the Mann Whitney U test. When comparing two independent samples, when the outcome is not normally distributed and the samples are small, a nonparametric test is appropriate. It is used to see the di
4 min read
Spearman Correlation Testing in R Programming Correlation is a key statistical concept used to measure the strength and direction of the relationship between two variables. Unlike Pearsonâs correlation, which assumes a linear relationship and continuous data, Spearmanâs rank correlation coefficient is a non-parametric measure that assesses how
3 min read
Kruskal-Wallis test in R Programming The KruskalâWallis test in R Programming Language is a rank-based test that is similar to the MannâWhitney U test but can be applied to one-way data with more than two groups. It is a non-parametric alternative to the one-way ANOVA test, which extends the two-samples Wilcoxon test. A group of data s
4 min read
Bartlettâs Test in R Programming In statistics, Bartlett's test is used to test if k samples are from populations with equal variances. Equal variances across populations are called homoscedasticity or homogeneity of variances. Some statistical tests, for example, the ANOVA test, assume that variances are equal across groups or sam
5 min read