Skip to content
Prev 387056 / 398502 Next

dependent nested for loops in R

Hi
I have made the sample code again. Could you please guide how to use
vectorization for variables whose next value depends on the previous one?

w = NULL

for(j in 1:1000)

{

  z = NULL

  x = rnorm(2000)

  z[1] = x[1]

  for(i in 2:2000)

  {

    z[i] = x[i]+5*z[i-1]

    if(z[i]>4 | z[i]<1) {

      w[j]=i

    } else {

      w[j] = 0

    }

  }

}


On Sun, Jan 31, 2021 at 10:01 AM David Winsemius <dwinsemius at comcast.net>
wrote: