r - Loops and ANOVA -
so have done analysis in sas , trying replicate in r new in r, know virtually nothing right now. have tried bunch of things seem error everywhere go. try things because figure if can make work on small scale can extrapolate larger scale.
basically have huge data set subjects have value metabolite. want run anova test on these metabolites, there 600+ of them. want find p-values , put them nice table metabolite label , p-value. here example of data like.
subject # treatment antibiotic metabolite1 metabolite2.... metabolite600 mg_1 md no 1.257 2.578 5.12 mg_2 ms 1ss 3.59 1.052 1.5201 mg_3 md1ss no 1.564 1.7489 1.310 etc... i know can run:
fit1 <- aov(metabolite1 ~ treatment * antibiotic, data=data1) to calculate first metabolite. trying loop try out. want know if can use aov function without having type or copy/paste , type in 1 600 everything.
in sas write macro variable , assign number when make name metabolite&i y value , fit&i save results. there way in r?
i've tried doing metabolite[i] (i in 1:20) doesn't work. there way reference in loop? proper syntax if there is?
edit: don't know how make simpler is, data set huge, literally have 3 lines of code right now.
library(gdata) testing = read.xls("~data1", sheet=1) fit1 <- aov(metabolite1 ~ treatment * antibiotic, data=data1) summary(fit1) this literally have. mentioned above tried doing
for (i in 1:20) { fit[i] <- aov(metabolite[i] ~ treatment * antibiotic, data=data1) } which not work. object metabolite not found. totally ignores reference value. trying start small @ first.
it's difficult debug following code without data, try following:
library(tidyverse) library(broom) data_nested <- data1 %>% gather(key = metabolitetype, value = metabolite, -subject, -treatment, -antibiotic) %>% group_by(metabolitetype) %>% nest() aov_fun <- function(df) { aov(metabolite ~ treatment * antibiotic, data = df) } (results <- data_nested %>% mutate(fit = map(data, aov_fun), tidy = map(fit, tidy)) %>% unnest(tidy))
Comments
Post a Comment