In most cases in R, if you are interested in using a for loop (not to mention nested loops), you are likely to be mistaken.
A general approach to solving your problem is to use the expand.grid function to create all input combinations, then use mapply to repeat each input combination multiple times and return the result list, then use do.call combine the result list into a data frame.
Your code should look something like this:
i <- c('A','B','C') j <- 1:10 k <- c('D','E') l <- c('F','G','H') params <- expand.grid(i, j, k, l, stringsAsFactors = FALSE)
You now have a data frame for all input combinations.
> head(params) Var1 Var2 Var3 Var4 1 A 1 DF 2 B 1 DF 3 C 1 DF 4 A 2 DF 5 B 2 DF 6 C 2 DF > tail(params) Var1 Var2 Var3 Var4 175 A 9 EH 176 B 9 EH 177 C 9 EH 178 A 10 EH 179 B 10 EH 180 C 10 EH
Now configure the function that mapply will use for each row of the params data frame.
# one_lm <- function(i, j, k, l) { form <- formula(paste0(i,"_PC_AB_",k, " ~ ", l)) result <- lm(form, data = schools, subset=Decile==j) list( col1 = i, col2 = j, estimate = round(coef(summary(result))[2,1],3), std_err = round(coef(summary(result))[2,2],3), z_value = round(coef(summary(result))[2,3],3), p_value = round(coef(summary(result))[2,4],3), pct_2.5 = round(confint(result)[2,1],2), pct_97.5 = round(confint(result)[2,2],2), r_square = round(summary(result)$r.squared, 3) ) }
Now use mapply to process each combination one at a time and return a list of ratings, std_err, etc. for each row.
result_list <- mapply(one_lm, params[,1], params[,2], params[,3], params[,4], SIMPLIFY = FALSE)
You can then combine all of these lists into a data frame using the do.call and rbind .
results <- do.call(rbind, result_list)