How do you perform a Mann-Whitney U Test in R?

The Mann-Whitney U Test is a non-parametric statistical test used to compare the median values of two independent groups. In R, the test can be performed by using the “wilcox.test” function, which takes in the two groups of data as input. The function then calculates the U statistic and corresponding p-value, which can be interpreted to determine if there is a significant difference between the two groups. It is important to note that the Mann-Whitney U Test assumes that the two groups have similar distributions and are independent from each other. Additionally, the test can be further customized by specifying options such as alternative hypothesis and continuity correction. Overall, the Mann-Whitney U Test in R provides a robust and efficient method for comparing two independent groups without making any assumptions about the underlying distribution of the data.

Perform a Mann-Whitney U Test in R


A (sometimes called the Wilcoxon rank-sum test) is used to compare the differences between two independent samples when the sample distributions are not and the sample sizes are small (n <30).

It is considered to be the nonparametric equivalent to the .

This tutorial explains how to perform a Mann-Whitney U test in R.

Example: Mann-Whitney U Test in R

Researchers want to know whether or not a new drug is effective at preventing panic attacks. A total of 12 patients are randomly split into two groups of 6 and assigned to receive the new drug or the placebo. The patients then record how many panic attacks they have over the course of one month.

The results are shown below:

NEW DRUG PLACEBO
3 4
5 8
1 6
4 2
3 1
5 9

Conduct a Mann-Whitney U Test to determine if there is a difference in the number of panic attacks for the patients in the placebo group compared to the new drug group. Use a .05 level of significance. 

There are two different ways to perform the Mann-Whitney U test, but both methods use the wilcox.test() function and both lead to the same outcome.

Option 1: Enter the data as two separate vectors.

#create a vector for each group
new <- c(3, 5, 1, 4, 3, 5)
placebo <- c(4, 8, 6, 2, 1, 9)

#perform the Mann Whitney U test
wilcox.test(new, placebo)

#output
Wilcoxon rank sum test with continuity correction

data:  new and placebo
W = 13, p-value = 0.468
alternative hypothesis: true location shift is not equal to 0

Option 2: Enter the data into one data frame with two columns. One column contains the number of panic attacks and the other contains the group.

#create a data frame with two columns, one for each group
drug_data <- data.frame(attacks = c(3, 5, 1, 4, 3, 5, 4, 8, 6, 2, 1, 9),
                        drug_group = c(rep("old", 6), rep("placebo", 6)))

#perform the Mann Whitney U test
wilcox.test(attacks~drug_group, data = drug_data)

#output
data:  attacks by drug_group
W = 13, p-value = 0.468
alternative hypothesis: true location shift is not equal to 0

Notice that both methods lead to the exact same result. Namely, the test statistic is W = 13 and the corresponding p-value is 0.468.

Since the p-value is greater than 0.05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that the number of panic attacks experienced by patients in the placebo group is different from the new drug group.

Notes on Using Wilcox.test()

For example, suppose we’d like to test the hypothesis that the new drug leads to less panic attacks than the placebo. In this case, we could specify alternative=”less” in our wilcox.test() function:

#create a vector for each group
new <- c(3, 5, 1, 4, 3, 5)
placebo <- c(4, 8, 6, 2, 1, 9)

#perform the Mann Whitney U test, specify alternative="less"
wilcox.test(new, placebo, alternative="less")

#output
	Wilcoxon rank sum test with continuity correction

data:  new and placebo
W = 13, p-value = 0.234
alternative hypothesis: true location shift is less than 0

Notice that the test statistic is still W = 13, but the p-value is now 0.234, which is exactly half as large as the previous p-value for the two-sided test.

Since the p-value is still greater than 0.05, we would still fail to reject the null hypothesis.

We do not have sufficient evidence to say that the number of panic attacks experienced by patients in the new drug group was less than that of the patients in the placebo group.

Additional Resources

x