如何将 table 从 R 导出到乳胶并包含维度名称?
How to export a table from R to latex and include dimension names?
我有一个简单的 table,其中包含要从 R 导出到 Latex 的维度名称。我正在寻找一种不需要在乳胶中进行额外编辑的直接方法。这看起来应该很容易,但我发现了很多关于这个主题的其他未解决的问题。
我尝试使用 Hmisc
latex
命令,在 this post. It yields ! LaTeX Error: Illegal character in array arg.
as the OP states, but the question remains unresolved. This post 之后有一堆 table 选项,但我没有看到维度名称是已解决。
R-code
library(Hmisc)
latex(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "")
输出
%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&$&[=12=]$&[=12=]$&[=12=]$\tabularnewline
Middle Atlantic&$&[=12=]$&[=12=]$&[=12=]$\tabularnewline
South Atlantic&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
East South Central&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
West South Central&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
East North Central&[=12=]$&[=12=]$&$&[=12=]$\tabularnewline
West North Central&[=12=]$&[=12=]$&$&[=12=]$\tabularnewline
Mountain&[=12=]$&[=12=]$&[=12=]$&$\tabularnewline
Pacific&[=12=]$&[=12=]$&[=12=]$&$\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}
Latex 渲染后的错误消息
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}]
通过添加对齐 &\multicolumn{1}
{l}{Y}{Northeast}
来解决错误会产生没有维度名称的 table在适当的地方。
期望输出
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
还尝试使用 memisc
package,它同样会产生 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))
如果您想将其直接写入文件,可以在 print.xtable
中使用 file
参数。这是生成的 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}
PDF 结果:
我有一个简单的 table,其中包含要从 R 导出到 Latex 的维度名称。我正在寻找一种不需要在乳胶中进行额外编辑的直接方法。这看起来应该很容易,但我发现了很多关于这个主题的其他未解决的问题。
我尝试使用 Hmisc
latex
命令,在 this post. It yields ! LaTeX Error: Illegal character in array arg.
as the OP states, but the question remains unresolved. This post 之后有一堆 table 选项,但我没有看到维度名称是已解决。
R-code
library(Hmisc)
latex(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "")
输出
%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&$&[=12=]$&[=12=]$&[=12=]$\tabularnewline
Middle Atlantic&$&[=12=]$&[=12=]$&[=12=]$\tabularnewline
South Atlantic&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
East South Central&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
West South Central&[=12=]$&$&[=12=]$&[=12=]$\tabularnewline
East North Central&[=12=]$&[=12=]$&$&[=12=]$\tabularnewline
West North Central&[=12=]$&[=12=]$&$&[=12=]$\tabularnewline
Mountain&[=12=]$&[=12=]$&[=12=]$&$\tabularnewline
Pacific&[=12=]$&[=12=]$&[=12=]$&$\tabularnewline
\hline
\end{tabular}\end{center}
\end{table}
Latex 渲染后的错误消息
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}]
通过添加对齐 &\multicolumn{1}
{l}{Y}{Northeast}
来解决错误会产生没有维度名称的 table在适当的地方。
期望输出
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
还尝试使用 memisc
package,它同样会产生 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))
如果您想将其直接写入文件,可以在 print.xtable
中使用 file
参数。这是生成的 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}
PDF 结果: