Nested latex table with stargazer

1.7k Views Asked by At

I have a list of dataframes like these:

library(plyr)
mt_list <- dlply(mtcars, .(cyl), data.frame)
names(mt_list) <- c("four", "six", "eight")

I want to create a nested latex table using the stargazer package. By nested, I mean I want to combine three dataframes within a single table and give each of the three dataframe's their own title within the table. The latex table should look this (photoshopped image):

enter image description here

Is it possible to create a table that looks like this from within R? I'm specifically interested in using the stargazer package to do this.

2

There are 2 best solutions below

4
On

Not exactly what you are looking for but should be a good start.

stargazer(mt_list,type='text',summary=FALSE,
          title=c('Six','Four','Eight'))
## 
## Six
## =========================================================
## mpg    cyl  disp   hp  drat   wt    qsec  vs am gear carb
## ---------------------------------------------------------
## 22.800  4    108   93  3.850 2.320 18.610 1  1   4    1  
## 24.400  4  146.700 62  3.690 3.190   20   1  0   4    2  
## 22.800  4  140.800 95  3.920 3.150 22.900 1  0   4    2  
## 32.400  4  78.700  66  4.080 2.200 19.470 1  1   4    1  
## 30.400  4  75.700  52  4.930 1.615 18.520 1  1   4    2  
## 33.900  4  71.100  65  4.220 1.835 19.900 1  1   4    1  
## 21.500  4  120.100 97  3.700 2.465 20.010 1  0   3    1  
## 27.300  4    79    66  4.080 1.935 18.900 1  1   4    1  
## 26      4  120.300 91  4.430 2.140 16.700 0  1   5    2  
## 30.400  4  95.100  113 3.770 1.513 16.900 1  1   5    2  
## 21.400  4    121   109 4.110 2.780 18.600 1  1   4    2  
## ---------------------------------------------------------
## 
## Four
## =========================================================
## mpg    cyl  disp   hp  drat   wt    qsec  vs am gear carb
## ---------------------------------------------------------
## 21      6    160   110 3.900 2.620 16.460 0  1   4    4  
## 21      6    160   110 3.900 2.875 17.020 0  1   4    4  
## 21.400  6    258   110 3.080 3.215 19.440 1  0   3    1  
## 18.100  6    225   105 2.760 3.460 20.220 1  0   3    1  
## 19.200  6  167.600 123 3.920 3.440 18.300 1  0   4    4  
## 17.800  6  167.600 123 3.920 3.440 18.900 1  0   4    4  
## 19.700  6    145   175 3.620 2.770 15.500 0  1   5    6  
## ---------------------------------------------------------
## 
## Eight
## =========================================================
## mpg    cyl  disp   hp  drat   wt    qsec  vs am gear carb
## ---------------------------------------------------------
## 18.700  8    360   175 3.150 3.440 17.020 0  0   3    2  
## 14.300  8    360   245 3.210 3.570 15.840 0  0   3    4  
## 16.400  8  275.800 180 3.070 4.070 17.400 0  0   3    3  
## 17.300  8  275.800 180 3.070 3.730 17.600 0  0   3    3  
## 15.200  8  275.800 180 3.070 3.780   18   0  0   3    3  
## 10.400  8    472   205 2.930 5.250 17.980 0  0   3    4  
## 10.400  8    460   215   3   5.424 17.820 0  0   3    4  
## 14.700  8    440   230 3.230 5.345 17.420 0  0   3    4  
## 15.500  8    318   150 2.760 3.520 16.870 0  0   3    2  
## 15.200  8    304   150 3.150 3.435 17.300 0  0   3    2  
## 13.300  8    350   245 3.730 3.840 15.410 0  0   3    4  
## 19.200  8    400   175 3.080 3.845 17.050 0  0   3    2  
## 15.800  8    351   264 4.220 3.170 14.500 0  1   5    4  
## 15      8    301   335 3.540 3.570 14.600 0  1   5    8  

## ---------------------------------------------------------
3
On

This is just a demonstration of @agstudy's solutions correctness. If yopu set type ="latex" and the create a tex document with a document class preamble and drop into a TeX processor you get:

\documentclass[a4paper,12pt]{article}
\begin{document}
% Table created by stargazer v.4.5.3 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Fri, Jan 03, 2014 - 11:25:30
\begin{table}[!htbp] \centering 
  \caption{Six} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccccccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ 
\hline \\[-1.8ex] 
$22.800$ & $4$ & $108$ & $93$ & $3.850$ & $2.320$ & $18.610$ & $1$ & $1$ & $4$ & $1$ \\ 
$24.400$ & $4$ & $146.700$ & $62$ & $3.690$ & $3.190$ & $20$ & $1$ & $0$ & $4$ & $2$ \\ 
snipped extraneous lines
$15.800$ & $8$ & $351$ & $264$ & $4.220$ & $3.170$ & $14.500$ & $0$ & $1$ & $5$ & $4$ \\ 
$15$ & $8$ & $301$ & $335$ & $3.540$ & $3.570$ & $14.600$ & $0$ & $1$ & $5$ & $8$ \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table} 

\end{document}

Why only two out of 3 tables? This produced a two page pdf when I did the typesetting and I don't think SO can handle pdf's so I converted to tiff. The tiff output was 16 MB so uploading as the tiff file as image file seemed kind of ridiculous. Png-format cannot handle multipage files so only two tables shown. enter image description here