My dataset consists of
cb <- data.frame(group = ("A", "B", "C", "D", "E"),
WC = runif(100, 0, 100),
Ana = runif(100, 0, 100),
Clo = runif(100, 0, 100))
str(cb)
data.frame: 66936 obs of 89 variables:
$group: Factor w/ 5 levels "A", "B", "C" ...
$WC: int 19 28 35 92 10 23...
$Ana: num 17.2 48 35.4 84.2
$ Clo: num 37.2 12.1 45.4 38.9
....
Now I want to perform multiple Wilcox tests on the $group, so that it looks like this in the end:
commands:
wilcox.test(cb$WC[cb$group == "A"], cb$WC[cb$group == "B"])
wilcox.test(cb$WC[cb$group == "A"], cb$WC[cb$group == "C"])
wilcox.test(cb$WC[cb$group == "A"], cb$WC[cb$group == "D"])
wilcox.test(cb$WC[cb$group == "A"], cb$WC[cb$group == "E"])
....
inserting the p-value:
WC A B C D E
A 1 0.12 0.03 0.2 0.42
B 0.12 1 0.1 0.07 0.1
C 0.03 0.1 1 0.2 0.3
D 0.2 0.07 0.2 1 0.1
E 0.42 0.1 0.3 0.1 1
Ana A B C D E
A 1 0.12 0.2 0.39 0.1
B 0.12 1 0.1 0.07 0.1
C ...
D
E
...
I have a for loop of a prior question, multiple t-tests, but i struggle to adapt it to this task, because the Wilcox-Test is so different in design. Here is the for loop I used for the t-test:
res <- matrix(NA, ncol=5,
dimnames=list(NULL, c("group", "col", "statistic", "estimate", "p.value")))
gr <- levels(cb$group)
for(cl in 2:ncol(cb)){
for(grp in gr){
temp <- cb[cb$group == grp, cl]
res <- rbind(res, c(grp, colnames(cb)[cl],
unlist(t.test(temp, mu = mean(cb[,cl]), alternative="two.sided"))[c(1, 5, 3)]))
}
}
Do you have an idea how to change this for loop to perform a wilcox test?