Dr.PhilCol
Dr.PhilCol

Reputation: 21

Creating a loop with compare_means

I am trying to create a loop to use compare_means (ggpubr library in R) across all columns in a dataframe and then select only significant p.adjusted values, but it does not work well.

Here is some code

head(df3)
sampleID    Actio   Beta    Gammes  Traw    Cluster2
gut10   10  2.2 55  13  HIGH
gut12   20  44  67  12  HIGH
gut34   5.5 3   89  33  LOW
gut26   4   45  23  4   LOW


library(ggpubr)
data<-list()
for (i in 2:length(df3)){
data<-compare_means(df3[[i]] ~ Cluster2,  data=df3, paired = FALSE,p.adjust.method="bonferroni",method = "wilcox.test") 
}
Error: `df3[i]` must evaluate to column positions or names, not a list

I would like to create an output to convert in dataframe with all the information contained in compare_means output Thanks a lot

Upvotes: 1

Views: 659

Answers (1)

Susana Silva
Susana Silva

Reputation: 21

Try this:

library(ggpubr)
data<-list()
for (i in 2:(length(df3)-1)){
new<-df3[,c(i,"Cluster2")]
colnames(new)<-c("interest","Cluster2")
data<-compare_means(interest ~ Cluster2,  data=new, paired = FALSE,p.adjust.method="bonferroni",method = "wilcox.test") 
}

Upvotes: 2

Related Questions