# Wilcoxon signed-rank test in R

I am trying to duplicate a Wilcoxon signed-rank test example in this Wikipedia post using R.

The data is as follows:

after = c(125, 115, 130, 140, 140, 115, 140, 125, 140, 135)
before = c(110, 122, 125, 120, 140, 124, 123, 137, 135, 145)
sgn = sign(after-before)
abs = abs(after - before)
d = data.frame(after,before,sgn,abs)
d$rank = rank(replace(abs,abs==0,NA), na='keep') d$multi = d$sgn * d$rank

(W=abs(sum(d$multi, na.rm = T))) W = 9  However, the test statistic value that R produces (undoubtedly due to some mistake that I am making in setting up the function or interpreting the output) is: wilcox.test(d$before,d$after, paired = T, alternative = "two.sided", correct=F) Wilcoxon signed rank test data: d$before and d$after V = 18, p-value = 0.5936 alternative hypothesis: true location shift is not equal to 0  This (V=18) is different from the 9 value in the Wikipedia post. What am I missing? • You didn't report the warnings: "Warning messages: 1: In wilcox.test.default(d$before, d$after, paired = T, alternative = "two.sided", : cannot compute exact p-value with ties" AND 2: In wilcox.test.default(d$before, d\$after, paired = T, alternative = "two.sided", : cannot compute exact p-value with zeroes
– DWin
Aug 12, 2016 at 0:45
• @42- I didn't think it was relevant in the way that it wouldn't affect the calculation of the test statistic. But I may be wrong. Feel free to add it as an edit, if you think it could help. Aug 12, 2016 at 0:48
• It seem relevant in that the fact that one particular test in your cited article had one result but that another test also described in that article is used by the R wilcox.test. YOU SHOULD INVESTIGATE THE CAUSE OF A WARNING.
– DWin
Aug 12, 2016 at 0:52

R reports the V-statistic, which is the sum of the positive ranks. The Wikipedia example computes it slightly differently, as the sum of all ranks, regardless of sign. In other words, both versions are correct (and equivalent). This CrossValidated post might be helpful.

Stumbled upon this and found it incredibly useful as well as the other CrossValidated post mentioned by @jdobres.

Just to expand a little on the answer above which is correct but in hopes that it will help someone in the future because it was very confusing at least to me initially.

Note that in R if you reverse the entry order of the paired variables you will get different V values but the same p value. That's what brought me here. If they reported the W statistic you would expect to see a sign change not a different magnitude as you get with V.

wilcox.test(d$$before, d$$after, paired = T)
wilcox.test(d$$after, d$$before, paired = T)


But it all comes back together, since as the Wikipedia article notes S is equal to the total rank sum (having removed the zero difference entries) so our S is:

sum(rank(1:9))


So the two possible V statistics are bound to add to 45 (18 & 27). And the difference between them is the W statistic (9). Finally back to the Wikipedia example we can now easily divine Siegel's T statistic, it is the smaller of the two sums of ranks of given sign; therefore, T would equal 3+4+5+6=18 (or the smaller of the two V's we could compute).