1

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.

  • Do you really need a `while` loop? Or can you just use, for example, `replicate` to run the prediction n times? That would give you a nice list with all the prediction results – talat Oct 15 '19 at 07:16
  • @docendodiscimus Unfortunately, I can not use `replicate` here, because the condition of the while loop must be met to run the model. I left out the actual code to make it more simple. –  Oct 15 '19 at 07:20
  • 1
    Have you looked into `?append`? – jay.sf Oct 15 '19 at 07:23

4 Answers4

1

The comment from @jay.sf helped me finding the list.append function from the rlist package that works as expected.

For the example of my question:

i <- 0
my_model <- list()
while(i < 5) {
  i <- i + 1
  my_model <- list.append(my_model, i) 
}

Or to refer to the nice example of Zhiqiang Wang:

i <- 0
my_model <- list()
while(i < 5) {
  i <- i + 1
  my_model <- list.append(my_model, lm(mpg ~ cyl, data = mtcars))
}

EDIT

The both answers are very promissing but they do not work for me because in every while loop step a dataframe is created and obviously this approach does not work for dataframes. See here:

my_model <- list()
my_model[1] <- data.frame(matrix(rnorm(10*10), ncol= 10))
str(my_model)
List of 1
 $ : num [1:10] -1.023 -0.548 -0.534 -1.444 2.471 ...

Fortunately, list.append works here as expected, too:

i <- 0
my_model <- list(NA)
while(i < 5) {
  i <- i + 1
  my_model <- list.append(my_model, data.frame(matrix(rnorm(10*10), ncol= 10)))
}
0

Initialize an empty list then put every output in it.

my_model = list()
i <- 0
while(i < 5) {
  i <- i + 1
  my_model[i] <- i
}

which gives:

> my_model
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5
BetterCallMe
  • 636
  • 7
  • 15
0

How about this:

my_model <- list()
i <- 0
while(i < 5) {
  i <- i + 1
  my_model[i] <- lm(mpg ~ cyl, data = mtcars)
}
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
0

Isn't the easiest way of doing things in this case the use of the apply family, like so:

my_model <- lapply(0:5,function(i){i})

This results in a list, which includes the result of the function, in this case the i.

Edit: Does this do the job

fun.list <- function(my.i){
  my_model <- lapply(0:my.i,function(i){i})
  return(my_model)
}


i <- 0 
repeat{
  my_model <- fun.list(my.i=i)
  if (i > 4) break
  i <- i + 1
}

The resulting list looks like this

>my_model
[[1]]
[1] 0

[[2]]
[1] 1

[[3]]
[1] 2

[[4]]
[1] 3

[[5]]
[1] 4

[[6]]
[1] 5
hannes101
  • 2,410
  • 1
  • 17
  • 40
  • I need to run a while loop because in each step of the while loop some objects change and the next stept is only of any sense if the condition of the while loop is still met. Once the condition is not met the (while) loop should stopp. –  Oct 15 '19 at 09:10
  • Perhaps you can use `repeat` to break the call to `lapply` if the condition is met and you can thus get rid of the while loop. Here is an example https://stackoverflow.com/questions/20507247/r-repeat-function-until-condition-met – hannes101 Oct 15 '19 at 09:13
  • Additionally, pehaps make the MWE more like the actual code. At the moment it is apparently too easy to reflect the needed solution. – hannes101 Oct 15 '19 at 15:35
  • @make the MWE more like the actual code: I disagree here. Sometimes people do that and others end up understanding a (huge) code, not knowing what the question is. In my opinion one should extract the actual problem and ask that rather then copy&paste some code. In my case, it does not matter whether I run a linear model or a logistic regression, all that matters is how to save each step of a while loop as elements of a list and that is what I asked for. –  Oct 16 '19 at 06:06
  • 1up for the interesting answer how to solve it without a while loop. –  Oct 16 '19 at 07:26