Skip to content
Prev 387054 / 398502 Next

dependent nested for loops in R

Hi
I have very large dependent nested for loops that are quite expensive
computationally. It takes weeks and does not end to give me results. Could
anyone please guide how could I use apply function or any other suggestion
for such big and dependent loops in R? A sample code is as follows.

w = NULL
for(j in 1:1000)
{
  x = rnorm(2000)
  z = x[1]
  for(i in 2:2000)
  {
    z = x[i]+5*z[i-1]
    if(z>4 | z<1) {
      w[j]=i
      break
    } else {
      w[j] = 0
    }
  }
}


Thank you