1

I would like to produce a LaTeX table containing the mean and standard deviation of a a variable for different values of two other variables. That is a grid of numbers with two values for each combination with the standard deviation below the mean in parentheses. Ideally, I would also report the totals (i.e. the marginals) along the bottom and right of the table respectively.

So for the mtcars data:

library(datasets)
library(data.table)
 DT<-data.table(mtcars)
 DT[, list(mean_mpg = mean(mpg), sd_mpg=sd(mpg)),by=.(gear,carb)]

This produces the necessary data:

    gear carb mean_mpg   sd_mpg
 1:    4    4 19.75000 1.552417

 2:    4    1 29.10000 5.061620

 3:    3    1 20.33333 1.934770

 4:    3    2 17.15000 2.092048

 5:    3    4 12.62000 2.089737

 6:    4    2 24.75000 3.961060

 7:    3    3 16.30000 1.053565

 8:    5    2 28.20000 3.111270

 9:    5    4 15.80000       NA


10:    5    6 19.70000       NA

11:    5    8 15.00000       NA

But, I want to obtain output of the form:

    \begin{tabular}{l*4}
\multicolumn{4}{c}{Gear} \\
Carb & 3 & 4 & 5 \\ 
1 & 20.3 & 29.1 & \\

  & (1.9) & (5.1) &  \\

2 & 17.2 & 24.8 & 28.2 \\

  & (2.1) & (4.0) & (3.1) \\


4 & 12.6 & 19.8 & 15.8 \\

 &  (2.1) & (1.6) & (.) \\



6 & & & 19.7 \\
& & & (.) \\

8 &  & & 15 \\
& & & (.) \\

\end{tabular}

I have experimented with Stargazr, qwraps2, and report tools and none of them seem able to do what I need. The actual numbers are easily obtained, as described here: How to get summary statistics by group but given I need to produce lots of these tables manually rearranging them. Note, that the means are the focus not additional information unlike in this question Generate Cross-table in R with mean and SD

dothyphendot
  • 133
  • 5
  • Rule of thumb: [Never use stargazer, it’s utter garbage](https://stackoverflow.com/a/32035526/1968). Try [texreg](https://github.com/leifeld/texreg), by all accounts it’s excellent. – Konrad Rudolph Jun 03 '19 at 16:51
  • 1
    This is currently a broad question, but could probably be salvaged. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and all necessary code. – camille Jun 03 '19 at 17:15
  • @camille thanks for your suggestion - I have followed your advice. – dothyphendot Jun 03 '19 at 21:20
  • @KonradRudolph thanks for the tip -- looks great I'll look into it. – dothyphendot Jun 03 '19 at 21:20

0 Answers0