【问题标题】:How to export a table from R to latex and include dimension names?如何将表格从 R 导出到 Latex 并包含维度名称?
【发布时间】:2015-02-27 17:45:17
【问题描述】:

我有一个简单的表,其中包含我想从 R 导出到 Latex 的维度名称。我正在寻找一种不需要在乳胶中进行额外编辑的直接方法。这看起来应该很容易,但我发现了一些关于这个主题的其他未解决的问题。

我尝试在this post 之后使用Hmisc latex 命令。正如OP所述,它产生! LaTeX Error: Illegal character in array arg.,但问题仍未解决。 This post 有一堆表选项,但我没有看到维度名称被寻址。

R 代码

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&$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}

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} 来解决错误会生成一个在适当位置没有维度名称的表格。

期望的输出

                    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 错误。

【问题讨论】:

  • stackoverflow.com/questions/12800784/… 可能对 alt 有帮助:library(tables) ; tblr <- tabular(state.division ~ state.region); latex(tblr)
  • @user20650 这是一个有用的链接,谢谢。

标签: r latex


【解决方案1】:

您可以为此使用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 结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    相关资源
    最近更新 更多