title: Double For loops in R
body: <p>How do you properly write a double <code>for</code> loop in R?</p>
  
  <p>For example, in C I would do</p>
  
  <pre><code>int i, j;
  for (i = 1; i &lt; 6; i++) {
      for (j=i; j &lt;= 3; j++) {
          printf("%i,%i\n",i,j);
      }
      // Do more operations for i &gt; 3...
  }
  </code></pre>
  
  <p>which would generate the (artificial) sequence:</p>
  
  <pre><code>1,1
  1,2
  1,3
  2,2
  2,3
  3,3
  </code></pre>
  
  <p>In R you do not get the same behaviour when you write</p>
  
  <pre><code>for (i in 1:6) {
      for (j in i:3) {
          print(paste(i,j,sep=","))
      }
  }
  </code></pre>
  
  <p>so I've been reduced to doing something like</p>
  
  <pre><code>for (i in 1:6) {
      j &lt;- i
      while (j &lt;= 3) {
          print(paste(i,j,sep=","))
          j &lt;- j+1
      }
  }       
  </code></pre>
  
  <p>Is there a better way?</p>
  
  <p>As Shane mentioned, maybe I should make this clear: I am particularly interested in the code-style matching the mathematics to make it easier for students to understand. It seems that students are the most comfortable with <code>for</code> loops.</p>
  
  <p>In particular, I want my students to simulate a <a href="http://en.wikipedia.org/wiki/LIBOR_market_model" rel="noreferrer">LIBOR market model</a>. The dynamics of the forward rate are to be simulated under the same probability measure. As such, for each time step and each 
   forward rate, the appropriate drift correction \mu_i needs to be calculated and added.</p>