CornFlex
CornFlex

Reputation: 1

One-way ANOVA for loop: how do I initiate through multiple colums of a dataframe

I want to run more than 1000 different one way ANOVA's

I would like to see if the number of reads from a single miroRNA changes between four different groups. And I would like that from each of the more than 1000 miRNAs.

My tibble dataframe looks like this:

I have my 4 groups (YC, OC, YH, OH) and a different miRNA in each column.

enter image description here I tried a for-loop through which I expect R to iterate through the name of the miRNAs and then summaries an ANOVA table and TukeyHSD test:

    for(i in 2:ncol(test))
{column<-names(test[i])AVz<-summary(aov(test[,i]~Group,data = 
test))tk<-TukeyHSD((aov(test[,i]~Group,data = 
test)))print(column)print(AVz)print(tk)}

BUT this didn't work:

Error: unexpected symbol in "for(i in 2:ncol(test)){column<-names(test[i])AVz"

Upvotes: 0

Views: 114

Answers (1)

Rfanatic
Rfanatic

Reputation: 2290

If you would like to run aov you can use cbind

 formula <- as.formula(paste0("cbind(", paste(names(iris)[-5], collapse = ","), ") ~ Species"))
    
    fit <- aov(formula, data=iris)
    summary(fit)

Response Sepal.Length :
             Df Sum Sq Mean Sq F value    Pr(>F)    
Species       2 63.212  31.606  119.26 < 2.2e-16 ***
Residuals   147 38.956   0.265                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 Response Sepal.Width :
             Df Sum Sq Mean Sq F value    Pr(>F)    
Species       2 11.345  5.6725   49.16 < 2.2e-16 ***
Residuals   147 16.962  0.1154                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 Response Petal.Length :
             Df Sum Sq Mean Sq F value    Pr(>F)    
Species       2 437.10 218.551  1180.2 < 2.2e-16 ***
Residuals   147  27.22   0.185                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

 Response Petal.Width :
             Df Sum Sq Mean Sq F value    Pr(>F)    
Species       2 80.413  40.207  960.01 < 2.2e-16 ***
Residuals   147  6.157   0.042                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Upvotes: 0

Related Questions