I run a while
loop which does some prediction models. My problem is that using a while
loop overwrites the model in each step, leaving only the results of the last iteration. But I want to keep the models of each step in a list.
Here is a simple example.
i <- 0
while(i < 5) {
i <- i + 1
my_model <- i
}
Here, my_model
contains only the i
of the last step:
my_model
5
How can I create a list that contains the my_model
objects of each step? So my expected output is:
my_model_list <- list(1, 2, 3, 4, 5)
I need to achieve this expected output with a while
loop like the one above. All I came up with so far is using assign
to create objects in each step. But I hope there is some better solution.