title: R - multiple nested loops
body: <p>I am trying to write a nested loop code to simulate 10 columns of data in a data frame with 101 rows. The first row of data has been assigned as starting values. Each column should be different as my matrix r is generated from random normals; however, the resulting values in each column are exactly the same. To give some context for the looping indices:</p>
  
  <pre><code>tmax=100; ncol(pop_sims) = 12 (so a total of 10 iterations, 3-12); ncol(r) = 10
  
  for (i in 1:tmax){
   for (j in 3:ncol(pop_sims)){
     for(k in 1:ncol(r)){
  
     if (pop_sims[i,j]*exp(r[i,k]) &lt;2) {
      pop_sims[i+1,j]&lt;- 0} 
    else {
        pop_sims[i+1,j] &lt;- pop_sims[i,j]*exp(r[i,k])}
  }}} 
  </code></pre>
  
  <p>Any thoughts would be appreciated.</p>
  
  <p>UPDATE: Instead of working with the multiple loops, I omitted the use of the matrix r and simplified my loops.</p>
  
  <pre><code>for (i in 1:tmax){
   for (j in 1:10){
  
    if (pop_sims[i,j]*exp(r[i,j]) &lt;2) {
      pop_sims[i+1,j]&lt;- 0} 
    else {
        pop_sims[i+1,j] &lt;- pop_sims[i,j]*exp(rnorm(1,mean=0.02, sd=0.1))}
  }}
  </code></pre>