我有一个简单的表,其中包含我想要从R导出到Latex的维度名称.我正在寻找一种直接的方法,不需要在乳胶中进行额外的编辑.这似乎应该很容易,但我在这个主题上找到了许多其他未解决的问题.
我试过在这篇文章之后使用Hmisc latex命令.它产生OP状态,但问题仍然没有得到解决. 这篇文章有一堆表格选项,但我没有看到维度名称被解决.! LaTeX Error: Illegal character in array arg.
R-代码
library(Hmisc)
latex(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "")
Run Code Online (Sandbox Code Playgroud)
产量
%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}
Run Code Online (Sandbox Code Playgroud)
乳胶渲染后的错误消息
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}]
Run Code Online (Sandbox Code Playgroud)
通过添加对齐&\multicolumn{1}{l}来解决错误会{Y}{Northeast}产生一个在适当的位置没有维度名称的表.
期望的输出
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
Run Code Online (Sandbox Code Playgroud)
也尝试使用memisc包,同样产生Illegal character错误.
您可以使用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))
Run Code Online (Sandbox Code Playgroud)
如果要将此参数直接写入文件,可以使用该file参数print.xtable.这是生成的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}
Run Code Online (Sandbox Code Playgroud)
PDF结果:

| 归档时间: |
|
| 查看次数: |
855 次 |
| 最近记录: |