# Hypothesis test for difference in medians among more than two samples

### Question

The test scores of three groups of people are saved as separate vectors in R.

``````set.seed(1)
group1 <- rnorm(100, mean = 75, sd = 10)
group2 <- rnorm(100, mean = 85, sd = 10)
group3 <- rnorm(100, mean = 95, sd = 10)
``````

I want to know if there is a significant difference in the medians between these groups. I know that I could test group 1 versus group 2 using the Wilcoxon test, like so.

``````wilcox.test(group1, group2)
``````

However, this compares only two groups at a time, and I would like to compare all three simultaneously. I would like a statistical test that yields a p value at the 0.05 significance level. Could someone please help?

### Edit #1 – Mood’s median test

Following user Hibernating’s suggested answer, I tried Mood’s median test.

``````median.test <- function(x, y){
z <- c(x, y)
g <- rep(1:2, c(length(x), length(y)))
m <- median(z)
fisher.test(z < m, g)\$p.value
}

median.test(group1, group2)
``````

However, this approach allows me to test for a significant difference between the medians of only two groups at a time. I am not sure of how to use it to compare the medians of all three simultaneously.

### Edit #2 – Kruskal-Wallis test

User dmartin’s suggested answer appears to be more or less what I need, and allows me to test all three groups simultaneously.

``````kruskal.test(list(group1, group2, group3))
``````

### Edit #3

User Greg Snow helpfully notes in his answer that the Kruskal-Wallis test is appropriate as long as it makes strict assumptions that make it also a test of means.