1

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!

Dustin
  • 183
  • 7

1 Answers1

2

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)))
asaei
  • 491
  • 3
  • 5
  • OMG, this is amazing! It works on my real data, too! I never would have come at from this angle (I was stuck failing at writing for loops). Thank you so much!!! – Dustin Feb 23 '23 at 03:35
  • I have just posted a question for the next step of my process (CLDs) at https://stackoverflow.com/questions/75551506/in-r-is-there-a-way-to-loop-multcompview-functions-i-e-cld-through-lists-of-a – Dustin Feb 24 '23 at 00:04