dunn_test
wants a formula and you attempted to provide data or a mixture of both. You could patch your for
loop like this:
library("rstatix")
data <- iris
for (i in seq(1:4)) {
a <- colnames(data)
dtest <- dunn_test(data, as.formula(paste(a[i], a[5], sep="~")),
p.adjust.method="BH")
print(dtest)
print(i)
}
# # A tibble: 3 x 9
# .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
# * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
# 1 Sepal~ setosa versi~ 50 50 6.11 1.02e- 9 1.53e- 9 ****
# 2 Sepal~ setosa virgi~ 50 50 9.74 2.00e-22 6.00e-22 ****
# 3 Sepal~ versic~ virgi~ 50 50 3.64 2.77e- 4 2.77e- 4 ***
# [1] 1
# [...]
Another way is to use reformulate
and Vectorize
it, as well as the dunn_test
function.
dunn_testv <- Vectorize(dunn_test, vectorize.args="formula", SIMPLIFY=F)
reformulatev <- Vectorize(reformulate, vectorize.args="response")
res <- dunn_testv(iris, reformulatev("Species", names(iris)[1:4]), p.adjust.method="BH")
res
# $Sepal.Length
# # A tibble: 3 x 9
# .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
# * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
# 1 Sepal~ setosa versi~ 50 50 6.11 1.02e- 9 1.53e- 9 ****
# 2 Sepal~ setosa virgi~ 50 50 9.74 2.00e-22 6.00e-22 ****
# 3 Sepal~ versic~ virgi~ 50 50 3.64 2.77e- 4 2.77e- 4 ***
# [...]