0

I'm facing some trouble with this R looping. I imagine it's because R isn't so prepared to code this kind of looping.

Thank you in advance!

df <- data.frame("x1" = NULL)

a <- 1
j <- 1

while (a<201)
{
  for(i in 1:1156)
  {
    df[j,"x1"] <- j
    j <- j + 1
  }
  a <- a + 1
}

length(df$pontos)
df
Cettt
  • 11,460
  • 7
  • 35
  • 58
Felipe Dourado
  • 441
  • 2
  • 12

2 Answers2

2

There are no errors in the codes actually. If you put print(a) after a <- a + 1, you can see that the codes work but extremely slow. I think that is the reason why you thought the loop never ends.

Bowen
  • 95
  • 11
  • Yes, there are no erros but the code is extremely slow. – Felipe Dourado Mar 26 '19 at 13:15
  • 1
    @FelipeDourado You seem to create one-column data frame from 1 to 200 * 1156. As a more efficient method, you can just pass the `1:(200 * 1156)` as the argument to `data.frame`. – Bowen Mar 26 '19 at 13:19
0

It runs for me if you decrease the iterations in the inner for loop to 100. It might just be taking a while. Could the outer loop just be a for loop as well?