Tabulating groups by row and column in R, WITHOUT looping
<p>Please feel free to edit the title if there is a more clear one.  Here is what I am trying to do.</p>

<p>Without looping (because the dimensions of the matrix will be very large and loops are too slow.)</p>

<p>Given this matrix, A</p>

<pre><code>  A = rbind(
    c(2, 2, 1, 1, 2, 2, 2  ),
    c(2, 2, 3, 2, 2, 3, 2  ),
    c(1, 1, 1, 2, 2, 1, 1  ),
    c(1, 1, 1, 1, 1, 1, 1  ),
    c(1, 2, 1, 1, 1, 1, 1  ),
    c(2, 2, 1, 1, 1, 1, 2  ),
    'S'=c(1, 2, 3, 4, 5, 6, 7))

A = cbind(A,'R'=c(rep(1:2,3),NA))  

                 R
  2 2 1 1 2 2 2  1
  2 2 3 2 2 3 2  2
  1 1 1 2 2 1 1  1
  1 1 1 1 1 1 1  2
  1 2 1 1 1 1 1  1
  2 2 1 1 1 1 2  2
S 1 2 3 4 5 6 7 NA
</code></pre>

<p>We have the bottom row called “S” and the last column called “R”</p>

<p>I want to group by R and S and basically return a TABLE () function of each group, for example, </p>

<pre><code>S=1, R =1

         R
  **2**  1

  **1**  1

  **1**  1

S   1    
</code></pre>

<p>The tricky part is when not all values show up, I still need it to tabulate 0 for the third.  (So perhaps I need some workaround)...
Basically, the table would return:</p>

<pre><code>1 2 3
2 1 0 

I would like the resulting thing to be 

(R * S) x 3 


And the output would be:

2 1 0
1 2 0 
3 0 0 
2 1 0 
1 2 0 
2 1 0 
2 1 0 
1 2 0 
1 2 0 
2 0 1
2 1 0 
2 1 0 
2 0 1
1 2 0 
</code></pre>