title: Nested While-Loop - R
body: <p>So I have a table <code>m</code>, consisting of a random number of rows and columns. (can be any size)...</p>
  
  <p>I want to do this calculation against each rows/columns totals:</p>
  
  <pre><code>  r[i] * c[j] / n;
  </code></pre>
  
  <p>Where <code>r &lt;- rowSums(m);</code>, <code>c &lt;- colSums(m);</code> and <code>n &lt;- sum(m);</code></p>
  
  <p>I can do it with a double for-loop but I'm hoping to implement it now using while loops.</p>
  
  <p>I wasn't going to use while loops but seems the table size can differ, I figured it was wise too.</p>
  
  <p>I'm storing each value as it's found in a <code>test</code> vector.</p>
  
  <p>This is my attempt, but I'm messing up the indices:</p>
  
  <pre><code>while(i &lt; nrow(m)){
      while(j &lt; ncol(m)){
          test[i] &lt;- r[i]*c[j] / n;
          j=j+1;
          i=i+1;
      }
      j=j+1;
      i=i+1;
  }
  </code></pre>
  
  <p>Any guidance to help me sort out my loops would be much appreciated. Thanks in advance.</p>
  
  <p><strong>update</strong></p>
  
  <p>See below for an example and expected result:</p>
  
  <pre><code>    m &lt;- t(matrix(c(28,48,10,114), nrow=2, ncol=2));
      r &lt;- rowSums(m); #76 124 (sum of rows)
      c &lt;- colSums(m); #38 162 (sum of cols)
      n &lt;- sum(m);     #200 (sum of all cells)
  
      test &lt;- c(0, times length(m)); #empty vector/data frame
  
      #inside while loops, calc for each r and c indice:
      test[1] &lt;- 76 *38 /200 #first calc to test[i] where i=1
      test[2] &lt;- 124*38 /200
      test[3] &lt;- 76*162 /200
      test[4] &lt;- 124*162/200 #last calc to test[i] where i==length(m)
  </code></pre>