I'm new to loops and I have a problem with calling variable from i'th data frame.
I'm able to call each data frame correctly, but when I should call a specified variable inside each data frame problems come:
Example:
for (i in 1:15) {
assign(
paste("model", i, sep = ""),
(lm(response ~ variable, data = eval(parse(text = paste("data", i, sep = "")))))
)
plot(data[i]$response, predict.lm(eval(parse(text = paste("model", i, sep = ""))))) #plot obs vs preds
}
Here I'm doing a simple one variable linear model 15 times, which works just fine. Problems come when I try to plot the results. How should I call data[i] response?
assignandeval? You can use a list to store all your datasets.assign,evalorparse, there is an extremely high probability that there is a much better way to do this in R. As @VincentZoonekynd pointed out, use a list.lapplyfor the loop.data[i]is not the same as an object named "data<i>" where the <i> is meant to be a digit or sequence of digits,