title: Crosstable with two variables that displays (only) the mean, standard deviation and samplesize of a third variable in R
body: <p>i'm searching for an easy way to create a multivariat crosstable with two variables that only displays the mean, standard deviation and frequencies/samplesize <strong>of a third</strong> variable. In the best case with column sums and row sums.</p>
  <p>Example with the mtcars dataset (table with &quot;cyl&quot; and &quot;vs&quot;; mean, standard deviation and frequencies of &quot;hp&quot;):
  In Stata it would be <code>tabulate cyl vs, summarize(hp) freq mean sta</code>.</p>
  <p><a href="https://i.stack.imgur.com/3MaLb.png" rel="nofollow noreferrer">To make clear how it ideally should look like I've made an example (the results are just made up):</a></p>
  <p>or</p>
  <p><a href="https://i.stack.imgur.com/44WLE.png" rel="nofollow noreferrer">Example 2 (results also just made up)</a></p>
  <p>(Finally I have to transfer the table to LaTeX - ideally with stargazer - so it would be best of all if this were possible.)</p>
  <p>I found a lot of solutions for crosstables with means, but only for two variables. And certainly not with mean, std. dev. and frequencies in one table. You would be a huge help to me.</p>
  <p>edit: Now I tried this, but I don't know how to add standard deviation, column sums and row sums.</p>
  <pre><code>    library(tidyr)
  
  ct &lt;- mtcars %&gt;%
    group_by(cyl, vs) %&gt;%
    summarise(hp = mean(hp, na.rm=TRUE), .groups = &quot;drop&quot;) %&gt;%
    spread(vs, hp)
  ct
  
  ct %&gt;%
    adorn_rounding() %&gt;%
    adorn_ns(
      ns = mtcars %&gt;% # calculate the Ns on the fly by calling tabyl on the original data
        tabyl(cyl, vs)
    ) %&gt;%
    adorn_title(&quot;combined&quot;, row_name = &quot;Cylinders&quot;, col_name = &quot;Is Automatic&quot;)
  
  # stargazer(ct)
  </code></pre>
  <p>And I get this (means by group and frequencies in brackets):</p>
  <pre><code>  Cylinders/Is Automatic          0          1
  1                      4    91  (1)  81.8 (10)
  2                      6 131.7  (3) 115.2  (4)
  3                      8 209.2 (14)    NA  (0)
  </code></pre>
  <p>edit2:</p>
  <pre><code>library(tidyr)
  library(arsenal)
  tab1 &lt;- tableby(vs ~ hp, data=mtcars, strata = cyl, numeric.stats = c(&quot;meansd&quot;, &quot;N&quot;), test = FALSE)
  summary(tab1, text = TRUE)
  </code></pre>
  <p>This sort of works for me, but to get the exact table - as in my illustration - into LaTeX, I unfortunately have to do a lot of work by hand.</p>
  <p>Unfortunately <code>stargazer(tab1)</code> does not work at all, because of</p>
  <blockquote>
  <p>% Error: Unrecognized object type.</p>
  </blockquote>
  <p>.</p>
  <p><code>summary(tab1, text = &quot;latex&quot;)</code> does work, but as mentioned to get a more or less beautiful, publication quality table in LaTeX there's a lot of work by hand.
  Maybe it's too much to ask, but does anyone have any ideas?</p>