I have a data frame with one grouping factor (the first column) with multiple levels (more than two) and several columns with data. I want to apply the wilcox.test
The pairwise.wilcox.test function seems like it would be useful here; perhaps like this?
out <- lapply(2:6, function(x) pairwise.wilcox.test(d[[x]], d$group))
names(out) <- names(d)[2:6]
out
If you just want the p-values, you can go through and extract those and make a matrix.
sapply(out, function(x) {
p <- x$p.value
n <- outer(rownames(p), colnames(p), paste, sep='v')
p <- as.vector(p)
names(p) <- n
p
})
## var1 var2 var3 var4 var5
## 2v1 0.5414627 0.8205958 0.4851572 1 1.0000000
## 3v1 0.1778222 0.3479835 1.0000000 1 1.0000000
## 2v2 NA NA NA NA NA
## 3v2 0.5414627 0.3479835 0.3784941 1 0.6919826
Also note that pairwise.wilcox.test adjusts for multiple comparisons using the Holm method; if you'd rather do something different, look at the p.adjust parameter.