The Kruskal-Wallis test (also known as One-way ANOVA on ranks) can be used for comparison of two (or more) independent samples. It is a non-parametric test which does not require normality of distribution, and thus replaces Student’s t-test or the One-way ANOVA.

The function in R is kruskal.test(x, y) where x and y are two vectors containing two samples. Let’s take the same example as we have used for Student’s t-test:

kruskal.test(Location_A, Location_B)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Location_A and Location_B
## Kruskal-Wallis chi-squared = 6, df = 6, p-value = 0.4232

Here, the test results in a p-value greater than 0.05, thus indicating that the null hypothesis is to be accepted. The means are not significantly different.

More than 2 groups?

As mentioned further above, the Kruskal-Wallis test may be used to compare the means of more than 2 groups, and is thus a good alternative to the one-way ANOVA (parametric test) when normality of the sample is a problem. Often when working with ANOVAs, data are stored in dataframes. Here is an example taken from the section one-way ANOVA that illustrates how to use the Kruskal-Wallis test with a dataframe df:

size <- c(25,22,28,24,26,24,22,21,23,25,26,30,25,24,21,27,28,23,25,24,20,22,24,23,22,24,20,19,21,22)
location <- rep(c("ForestA", "ForestB", "ForestC"), each = 10)
df <- data.frame(size,location)
kruskal.test(size~location, data=df)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  size by location
## Kruskal-Wallis chi-squared = 10.371, df = 2, p-value = 0.005596

The p-value is less than 0.05, the null hypothesis is to be rejected. The group means are significantly different. However the test does not tell you which groups are different from the others… you’ll need a post-hoc test and pairwise comparisons to go further in the analysis.