How to perform Wilcoxon test for all columns in an R data frame?


Performing Wilcoxon test for all columns in an R data frame means that we want to use this test for single samples and the Wilcoxon test for single sample is used to test for the median of the sample, whether the median is equal to something or not. And if we do not provide any value then zero is the reference value. To perform Wilcoxon test for all columns can be done with the help of apply function and wilcox.test as shown in the below example.

Consider the below data frame −

Example

 Live Demo

x1<-rnorm(20,5,0.31)
x2<-rnorm(20,2,0.025)
x3<-rpois(20,4)
x4<-rpois(20,2)
x5<-rpois(20,5)
x6<-rpois(20,1)
x7<-round(rnorm(20,3,1.1),2)
x8<-round(rnorm(20,10,2.25),2)
df<-data.frame(x1,x2,x3,x4,x5,x6,x7,x8)
df

Output

     x1      x2        x3  x4  x5  x6    x7   x8
1 4.667097  2.032878   5   1   8   0   3.82   5.68
2 4.556913  1.952845   7   3  5    0   4.62   12.57
3 5.274511  1.947305   3   0  2    1   3.27   7.03
4 4.621090  1.960653   4   3  4    0   3.37   9.71
5 4.808041  1.955832   4   3  4    0   2.96   7.58
6 4.509070  2.084535   2   4  10   1   3.04   6.42
7 5.230658  1.981629   2   2  5   0    2.26   7.25
8 4.724433  1.986739   3   2  5   0   3.76   10.46
9 5.123489  1.959177   3   1  1   0   3.97   10.55
10 5.179769  1.970168  3  3   4   0   3.91   7.68
11 5.133287  2.006720  5   1   7   0  1.89   10.85
12 4.677813  2.007699  3   0   6   2  1.81   9.01
13 4.662342  2.064619  7   2  5    3  2.72   8.42
14 5.375585  1.994618  2   1  3    0  4.09   9.83
15 5.574414  1.997730  2   4   3    0 3.14   9.58
16 5.279330  1.985777  8   4   10  2  2.95   9.60
17 5.258145  2.019408  2   3   6   1  3.42   10.68
18 5.051640  2.017030  6   3   4   2  3.91   11.66
19 5.064925  2.007080  4   1   6   0  3.06   8.74
20 4.957406  1.964513  9   2   5   0  4.59   11.11

Performing Wilcoxon test on all columns of df −

apply(df,2,wilcox.test)
$x1
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0
$x2
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0
$x3
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 8.966e-05
alternative hypothesis: true location is not equal to 0
$x4
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 171, p-value = 0.0001896
alternative hypothesis: true location is not equal to 0
$x5


Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 9.095e-05
alternative hypothesis: true location is not equal to 0
$x6
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 28, p-value = 0.0206
alternative hypothesis: true location is not equal to 0
$x7
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 9.556e-05
alternative hypothesis: true location is not equal to 0
$x8
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0

Warning messages −

1: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
2: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
3: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with zeroes
4: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
5: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
6: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with zeroes
7: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties

Updated on: 06-Feb-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements