[英]How to export a table from R to latex and include dimension names?
I have a simple table with dimension names that I want to export from R to Latex. 我有一个简单的表,其中包含我想要从R导出到Latex的维度名称。 I'm looking for a straightforward way to do this that doesn't requiring additional editing in latex.
我正在寻找一种直接的方法,不需要在乳胶中进行额外的编辑。 This seems like it should be easy, but I've found a number of other unsolved questions on this topic.
这似乎应该很容易,但我在这个主题上找到了许多其他未解决的问题。
I've tried to use the Hmisc
latex
command following this post . 我试过在这篇文章之后使用
Hmisc
latex
命令。 It yields ! LaTeX Error: Illegal character in array arg.
它产量
! LaTeX Error: Illegal character in array arg.
! LaTeX Error: Illegal character in array arg.
as the OP states, but the question remains unresolved. 如OP所述,但问题仍未得到解决。 This post has a pile of table options, but I don't see that dimension names are addressed.
这篇文章有一堆表格选项,但我没有看到维度名称被解决。
R-code R-代码
library(Hmisc)
latex(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "")
Output 产量
%latex.default(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "")%
\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrrrr}
\hline\hline
\multicolumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}&\multicolumn{1}{l}{South}&\multicolumn{1}{Y}{North Central}&\multicolumn{1}{l}{West}\tabularnewline
\hline
New England&$6$&$0$&$0$&$0$\tabularnewline
Middle Atlantic&$3$&$0$&$0$&$0$\tabularnewline
South Atlantic&$0$&$8$&$0$&$0$\tabularnewline
East South Central&$0$&$4$&$0$&$0$\tabularnewline
West South Central&$0$&$4$&$0$&$0$\tabularnewline
East North Central&$0$&$0$&$5$&$0$\tabularnewline
West North Central&$0$&$0$&$7$&$0$\tabularnewline
Mountain&$0$&$0$&$0$&$8$\tabularnewline
Pacific&$0$&$0$&$0$&$5$\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}
Error Messages After Latex Rendering 乳胶渲染后的错误消息
Errors:
./test.tex:9: LaTeX Error: Illegal character in array arg. [...lumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}]
./test.tex:9: LaTeX Error: Illegal character in array arg. [...l}{South}&\multicolumn{1}{Y}{North Central}]
Addressing the errors by adding the alignment &\\multicolumn{1}
{l} {Y}{Northeast}
yields a table that doesn't have the dimension names in the appropriate place. 通过添加对齐
&\\multicolumn{1}
{l} {Y}{Northeast}
解决错误,会产生一个在适当的位置没有维度名称的表。
Desired Output 期望的输出
state.region
_____________________________________________________________
state.division Northeast South North Central West
_____________________________________________________________
New England 6 0 0 0
Middle Atlantic 3 0 0 0
South Atlantic 0 8 0 0
East South Central 0 4 0 0
West South Central 0 4 0 0
East North Central 0 0 5 0
West North Central 0 0 7 0
Mountain 0 0 0 8
Pacific 0 0 0 5
Also tried using the memisc
package , which similarly yields Illegal character
errors. 还尝试使用
memisc
包 ,类似地产生Illegal character
错误。
You can use xtable
for this (from the xtable package): 您可以使用
xtable
(来自xtable包):
# create your table
tab <- table(state.division, state.region)
# reassemble to put things where they need to be
tab2 <- cbind(rownames(tab), tab)
tab3 <- rbind(c("","\\multicolumn{4}{l}{state.region}", rep("",ncol(tab2)-2)),
c("state.division",colnames(tab2)[-1]),
tab2)
# print as xtable
library("xtable")
print(xtable(tab3), include.rownames = FALSE, include.colnames = FALSE, sanitize.text.function = I, hline.after = c(0,1,2,11))
You can use the file
argument in print.xtable
if you want to write this directly to a file. 如果要将此参数直接写入
file
可以使用print.xtable
的file
参数。 Here's the resulting LaTeX code: 这是生成的LaTeX代码:
% latex table generated in R 3.1.3 by xtable 1.7-4 package
% Thu Apr 23 21:25:44 2015
\begin{table}[ht]
\centering
\begin{tabular}{lllll}
\hline
& \multicolumn{4}{l}{state.region} & & & \\
\hline
state.division & Northeast & South & North Central & West \\
\hline
New England & 6 & 0 & 0 & 0 \\
Middle Atlantic & 3 & 0 & 0 & 0 \\
South Atlantic & 0 & 8 & 0 & 0 \\
East South Central & 0 & 4 & 0 & 0 \\
West South Central & 0 & 4 & 0 & 0 \\
East North Central & 0 & 0 & 5 & 0 \\
West North Central & 0 & 0 & 7 & 0 \\
Mountain & 0 & 0 & 0 & 8 \\
Pacific & 0 & 0 & 0 & 5 \\
\hline
\end{tabular}
\end{table}
And the PDF result: PDF结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.