title: Crosstable similar to Stata in R
body: <p>I am working with a UCLA <a href="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/sample.csv" rel="nofollow noreferrer">sample data set</a></p>
  
  <pre><code>&gt; read &lt;- read.table("http://www.ats.ucla.edu/stat/mult_pkg/faq/general/sample.csv",
            header=TRUE, sep=",", quote="\"")
  &gt; head(read)
    female read write math hon femalexmath
  1      0   57    52   41   0           0
  2      1   68    59   53   0          53
  3      0   44    33   54   0           0
  4      0   63    44   47   0           0
  </code></pre>
  
  <p>and I want to <code>crosstab</code> the variables <code>hon</code> with <code>female</code></p>
  
  <p>The <em>desired result</em> looks like this <code>stata</code> output:</p>
  
  <pre><code>           |        female
         hon |      male     female |     Total
  -----------+----------------------+----------
           0 |        74         77 |       151 
           1 |        17         32 |        49 
  -----------+----------------------+----------
       Total |        91        109 |       200 
  </code></pre>
  
  <p>Using <code>R</code>, I tried to use <code>xtabs</code></p>
  
  <pre><code>&gt; xtabs(female~hon, data = read)
  hon
   0  1 
  77 32 
  </code></pre>
  
  <p>and <code>reshape2</code></p>
  
  <pre><code>&gt; library(reshape2)
  &gt; melt &lt;- melt(read, id="female")
  &gt; dcast(melt, variable ~ female, sum, subset = .(variable == "hon"))
  hon
   0  1 
  77 32 
  </code></pre>
  
  <p>and <code>table</code></p>
  
  <pre><code>&gt; table(read$hon, read$female)
       0  1
    0 74 77
    1 17 32
  </code></pre>
  
  <p>but this is only a part of the desired result</p>
  
  <p>I'd like to include the <code>non-female</code> (=<code>male</code>) values and calculate the total numbers, and assign names appropriately.</p>
  
  <p>Am I missing an easy function for that in <code>R</code>?</p>
  
  <p>I have seen this post <a href="https://stackoverflow.com/questions/13043817/mimic-tabulate-command-from-stata-in-r">Mimic tabulate command from Stata in R</a>, but since the code in this question did not contain the library <code>gmodels</code> for <code>CrossTable</code>, I couldn't apply it. The output also looks different. </p>