I want to write a loop on R to perform linear regression on my dataset genes (= 210011 genes and 6 samples total; with columns the gene and the rows the samples) to identify how age and sex affect gene expression. I want to save the fitted value output from the linear regression in a new dataframe (generating basically a similar dataframe where on the columns there are the genes and in the rows the samples).
so the loop I wrote is:
genelist <- df %>% select(5:21011) #select only genes
for (i in 1:length(genelist)) {
formula <- as.formula(paste0(genelist[i], ' ~ age + sex'))
model <- lm(formula, data = df)
print(model$fitted.values)
}
but I am not able to save a new dataframe. I tried to follow this How to Loop/Repeat a Linear Regression in R
test <- list(); model <- list()
for (i in 1:length(genelist)) {
formula[[i]] = paste0(genelist[i], ' ~ age + sex')
model[[i]] = lm(formula[[i]], data = df)
}
but it gives me "list of 0" as results so I must have written something wrong. How can I modif my original code generating a new dataframe with the results?
thanks for help help!