I am trying to perform Dunn's test for Iris data. I want to loop over 4 columns and perform Dunn's test for each column on different species. However, when I try to get the column name it does not work. Can anybody tell me why?
library(rstatix)
data<-iris
for (i in seq(1:4)) {
a<-colnames(data)
colname1 <-as.character(a[5])
colname2 <-as.character(a[i])
dtest<-data %>%
dunn_test( get(colname2) ~ get(colname1), p.adjust.method = "BH")
print(dtest)
print(i)
}
dunn_test
wants a formula and you attempted to provide data or a mixture of both. You could patch yourfor
loop like this:Another way is to use
reformulate
andVectorize
it, as well as thedunn_test
function.