rloopsfor-loopdunn.test

dunn's test to loop over columns of a data-frame


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)
}

Solution

  • 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 ***
    # [...]