Search code examples
rfor-loopbroom

In R how do you loop ANOVA and post-hoc Tukey tests through several variables and capture summaries in a data frame?


I have data that looks like this;

df <- data.frame(Treat = rep(LETTERS[1:4], 100, replace = TRUE),
                 A = rnorm(400),
                 B = rnorm(400),
                 C = rnorm(400),
                 D = rnorm(400),
                 E = rnorm(400),
                 F = rnorm(400),
                 G = rnorm(400),
                 H = rnorm(400))

I want to loop aov() and then TukeyHSD() through variables Treat A:H, and capture the summaries of both the ANOVA and the Tukey for each variable into a single data frame. I have gotten close looking at one answer here using broom::tidy, but cant figure out how to get the summaries into a single data frame (as opposed to printing them).

Any help is extremely appreciated!


Solution

  • Here is how you can do this :

    ## perform anova on dependent variables
    aov_res <- apply(df[,2:ncol(df)], 2, function(x) aov(x ~ Treat, data = df))
    
    # Apply Tukey's HSD test to the results of each ANOVA test
    tukey_res <- sapply(aov_res, function(x) TukeyHSD(x, "Treat", ordered = TRUE))
    
    
    # Convert the results of each ANOVA test into a tidy data frame using the broom package
    aov_res_df <- do.call(rbind, lapply(aov_res, broom::tidy))
    
    # Combine the results of the Tukey HSD tests into a single data frame
    tukey_res_df <- as.data.frame(do.call(rbind, Map(cbind, Name = names(tukey_res), tukey_res)))