basti41a
basti41a

Reputation: 193

R How to perform multiple anovas and Post-Hoc-Test over a list of df´s?

how can I perform multiple ANOVA´s and Post-Hoc-Tests over a list of dataframes? Assuming I have this type of data structure:

set.seed(2)
df1 <- data.frame(Number = c("No1", "No2", "No3", "No1", "No2", "No3", "No1", "No2", "No3" ),
                  value = sample(1:200, 9))

df2 <- data.frame(Number = c("No4", "No5", "No6", "No4", "No5", "No6", "No4", "No5", "No6" ),
                  value = sample(1:200, 9))

df3 <- data.frame(Number = c("No7", "No8", "No9", "No7", "No8", "No9", "No7", "No8", "No9" ),
                  value = sample(1:200, 9))

df <- list(df1 = df1,df2 = df2,df3 = df3)

With one single df I am able to perform the anova and also perform the HSD.test from the package agricolae

library(agricolae)
ANOVA <- aov(value ~ Number, data = df1)
HSD_Test <- agricolae::HSD.test(y = ANOVA,
                                trt = "Number", 
                                group = TRUE,
                                console = TRUE,
                                alpha = 0.3)
graphics::plot(HSD_Test)

But how can I do this with multiple df´s in the list? Ideally I would want to store the grouping variables (a,b,c etc) in a new column in the dataframes like this:

> df1
  Number value Group
1    No1    33     a
2    No2    18     a
3    No3     8     b
4    No1   164     a
5    No2    94     a
6    No3    37     b
7    No1   149     a
8    No2   118     a
9    No3    71     b

I do not want to combine the df´s in one big df and run then the ANOVA.

Upvotes: 1

Views: 382

Answers (1)

David Mu&#241;oz Tord
David Mu&#241;oz Tord

Reputation: 365

Something like that should put you on your way:

set.seed(2)
df1 <- data.frame(Number = c("No1", "No2", "No3", "No1", "No2", "No3", "No1", "No2", "No3" ),
              value = sample(1:200, 9))
df2 <- data.frame(Number = c("No4", "No5", "No6", "No4", "No5", "No6", "No4", "No5", "No6" ),
              value = sample(1:200, 9))
df3 <- data.frame(Number = c("No7", "No8", "No9", "No7", "No8", "No9", "No7", "No8", "No9" ),
              value = sample(1:200, 9))
df <- list(df1 = df1,df2 = df2,df3 = df3)

ex <- lapply(df,function(x) aov(value ~ Number, data = x))

Upvotes: 1

Related Questions