0

I need to compute a statistical analysis that compares control vs treatment for each gene, I've identified the type of stats that I need and it works if performed to one gene:

df <- data.frame(Gene = c("A","A","A","A","A","A","B","B","B","B","B","B"), 
                 Value =c(12.554595492,13.554595492,14.554595492,8.554595492,2.554595492,3.554595492,13.554595492,8.554595492,16.5444425154,11.5444425154,3.5444425154,4.5444425154), 
                 Treat=c("tre1","tre1","tre2","tre2","Control","Control","tre1","tre1","tre2","tre2","Control","Control"))


df$Treat <- as.factor(df$Treat)

A <-  df %>% filter("A"== Gene)

model <- aov(Value ~ Treat, data = A)
summary(model)
model_1 <- glht(model = model,linfct = mcp(Treat="Dunnett"))

summary(model_1)

output<-capture.output(summary(model_1), file=NULL, append=FALSE)

output_df <-as.data.frame(output)

I would like to automate this operation to all the genes in the df and generate a final df that collects the results of all summaries.

When I try to perform the code in a for loop, I get the error "Error in eval(predvars, data, env) : object 'Value' not found". Where is the code wrong?

df_list <- split(df,f=df$Gene) 


df_final <- data.frame()[1:16, ]

for(i in length(df_list)){
  
  model <- aov(Value ~ Treat, data = i)
  summary(model)
  
  model_1 <- glht(model = model,linfct = mcp(Treat="Dunnett"))
  summary(model_1)
  
  output<-capture.output(summary(model_1), file=NULL, append=FALSE)
  output_df <-as.data.frame(output)
  
  df_final <- cbind (df_final,output_df)
  
  
}

The only difference I found between the dataframe A and those in the list is the Value as number in A and as double in the list. Is this the reason? how to fix it?

Any help or suggestion is appreciated! Thanks.

Sedlin
  • 53
  • 4
  • 1
    `i` is just equal to 2. It doesn't change and it is not a data.frame in your current code. If you want to loop over all values, it should be something like `for(i in 1:length(df_list))` or `for(i in seq_along(df_list))` if you want the index. But then you need `df_list[[i]]` to get the data. Or you can just do `for (i in df_list)` if you actually want the data.frames vales in `i`. – MrFlick Jun 24 '22 at 15:22
  • @MrFlick I used for(i in df_list) and worked in with dummy data!! Thanks a lot – Sedlin Jun 24 '22 at 15:31

0 Answers0