title: Mimic tabulate command from Stata in R
body: <p>I'm trying to get a 2 way table in <strong>R</strong> similar to this one from Stata. I was trying to use <code>CrossTable</code> from <code>gmodels</code> package, but the table is not the same. Do you known how can this be done in <strong>R</strong>? </p>
  
  <p>I hope at least to get the frequencies from </p>
  
  <p>when cursmoke1 == "Yes" &amp; cursmoke2 == "No" and reversed</p>
  
  <p>In <strong>R</strong> I'm only getting totals from yes, no and NA.</p>
  
  <p>Here is the output: </p>
  
  <p>Stata </p>
  
  <pre><code>. tabulate cursmoke1 cursmoke2, cell column miss row
  
  
  +-------------------+
  | Key               |
  |-------------------|
  |     frequency     |
  |  row percentage   |
  | column percentage |
  |  cell percentage  |
  +-------------------+
  
     Current |
     smoker, |      Current smoker, exam 2
      exam 1 |        No        Yes          . |     Total
  -----------+---------------------------------+----------
          No |     1,898        131        224 |     2,253 
             |     84.24       5.81       9.94 |    100.00 
             |     86.16       7.59      44.44 |     50.81 
             |     42.81       2.95       5.05 |     50.81 
  -----------+---------------------------------+----------
         Yes |       305      1,596        280 |     2,181 
             |     13.98      73.18      12.84 |    100.00 
             |     13.84      92.41      55.56 |     49.19 
             |      6.88      35.99       6.31 |     49.19 
  -----------+---------------------------------+----------
       Total |     2,203      1,727        504 |     4,434 
             |     49.68      38.95      11.37 |    100.00 
             |    100.00     100.00     100.00 |    100.00 
             |     49.68      38.95      11.37 |    100.00 
  </code></pre>
  
  <p>R </p>
  
  <pre><code>&gt; CrossTable(cursmoke2, cursmoke1, missing.include = T, format="SAS")
  
  
     Cell Contents
  |-------------------------|
  |                       N |
  | Chi-square contribution |
  |           N / Row Total |
  |           N / Col Total |
  |         N / Table Total |
  |-------------------------|
  
  
  Total Observations in Table:  4434 
  
  
               | cursmoke1 
     cursmoke2 |        No |       Yes |        NA | Row Total | 
  -------------|-----------|-----------|-----------|-----------|
            No |      2203 |         0 |         0 |      2203 | 
               |  1122.544 |   858.047 |   250.409 |           | 
               |     1.000 |     0.000 |     0.000 |     0.497 | 
               |     1.000 |     0.000 |     0.000 |           | 
               |     0.497 |     0.000 |     0.000 |           | 
  -------------|-----------|-----------|-----------|-----------|
           Yes |         0 |      1727 |         0 |      1727 | 
               |   858.047 |  1652.650 |   196.303 |           | 
               |     0.000 |     1.000 |     0.000 |     0.389 | 
               |     0.000 |     1.000 |     0.000 |           | 
               |     0.000 |     0.389 |     0.000 |           | 
  -------------|-----------|-----------|-----------|-----------|
            NA |         0 |         0 |       504 |       504 | 
               |   250.409 |   196.303 |  3483.288 |           | 
               |     0.000 |     0.000 |     1.000 |     0.114 | 
               |     0.000 |     0.000 |     1.000 |           | 
               |     0.000 |     0.000 |     0.114 |           | 
  -------------|-----------|-----------|-----------|-----------|
  Column Total |      2203 |      1727 |       504 |      4434 | 
               |     0.497 |     0.389 |     0.114 |           | 
  -------------|-----------|-----------|-----------|-----------|
  </code></pre>