[英]Nested latex table with stargazer
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. 我想使用stargazer包创建一个嵌套的乳胶表。 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): 乳胶表应该看起来这个(photoshopped图像):
Is it possible to create a table that looks like this from within R? 是否可以在R中创建一个看起来像这样的表? I'm specifically interested in using the stargazer package to do this. 我特别感兴趣的是使用stargazer包来做到这一点。
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
## ---------------------------------------------------------
This is just a demonstration of @agstudy's solutions correctness. 这只是@ agstudy解决方案正确性的证明。 If yopu set type ="latex" and the create a tex document with a document class preamble and drop into a TeX processor you get: 如果yopu设置type =“latex”并创建带有文档类前导码的tex文档并放入TeX处理器,您将获得:
\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? 为什么只有3个表中的两个? 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. 当我进行排版时,这产生了两页pdf,我不认为SO可以处理pdf,所以我转换为tiff。 The tiff output was 16 MB so uploading as the tiff file as image file seemed kind of ridiculous. tiff输出为16 MB,因此像tiff文件一样上传为图像文件似乎有点荒谬。 Png-format cannot handle multipage files so only two tables shown. Png格式无法处理多页文件,因此只显示了两个表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.