【问题标题】:How to get dimnames in xtable.table output?如何在 xtable.table 输出中获取暗名?
【发布时间】:2012-10-09 13:08:56
【问题描述】:

我想在我的xtable 输出中标记尺寸。但是xtable的表格方法即使我手动指定给table,也不会输出维度标签:

set.seed(10)
d <- data.frame(x=sample(1:4),y=sample(1:4))
tb <- with(d, table(d,dnn=c("Xs","Ys")))
> tb
   Ys
Xs  1 2 3 4
  1 0 0 0 1
  2 0 1 0 0
  3 1 0 0 0
  4 0 0 1 0
> xtable(tb)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Tue Oct  9 09:06:10 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
  \hline
 & 1 & 2 & 3 & 4 \\ 
  \hline
1 &   0 &   0 &   0 &   1 \\ 
  2 &   0 &   1 &   0 &   0 \\ 
  3 &   1 &   0 &   0 &   0 \\ 
  4 &   0 &   0 &   1 &   0 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

检查xtable.table 的代码不会产生任何秘密。除了使用 multirow 手动构建它们之外,还有什么方法可以标记尺寸吗?

【问题讨论】:

    标签: r xtable


    【解决方案1】:

    tables 包的一个解决方案:

    library(tables) 
    
    tblr <- tabular((Xs = as.factor(x)) ~ (Ys = as.factor(y)), data = d)
    latex(tblr)
    
    \begin{tabular}{lcccc}
    \hline
     & \multicolumn{4}{c}{Ys} \\ 
    Xs  & 1 & 2 & 3 & \multicolumn{1}{c}{4} \\ 
    \hline
    1  & $0$ & $0$ & $0$ & $1$ \\
    2  & $0$ & $1$ & $0$ & $0$ \\
    3  & $1$ & $0$ & $0$ & $0$ \\
    4  & $0$ & $0$ & $1$ & $0$ \\
    \hline 
    \end{tabular}
    

    【讨论】:

      【解决方案2】:

      这不会根据维度的名称创建多行或多列标题,但至少会显示它们。

      print(xtable(format(ftable(tb))), 
            include.rownames=FALSE, include.colnames=FALSE,
            sanitize.text.function = function(x) {gsub('"',"",x)})
      

      给了

      % latex table generated in R 2.15.1 by xtable 1.7-0 package
      % Tue Oct 09 11:28:33 2012
      \begin{table}[ht]
      \begin{center}
      \begin{tabular}{llllll}
        \hline
        \hline
           & Ys & 1 & 2 & 3 & 4 \\ 
        Xs &      &     &     &     &     \\ 
        1  &      &   0 &   0 &   0 &   1 \\ 
        2  &      &   0 &   1 &   0 &   0 \\ 
        3  &      &   1 &   0 &   0 &   0 \\ 
        4  &      &   0 &   0 &   1 &   0 \\ 
         \hline
      \end{tabular}
      \end{center}
      \end{table}
      

      你也可以在“正确”的地方恢复水平线:

      print(xtable(format(ftable(tb))), 
            include.rownames=FALSE, include.colnames=FALSE,
            sanitize.text.function = function(x) {gsub('"',"",x)},
            hline.after = c(-1, 2, nrow(tb)+2))
      

      给予

      % latex table generated in R 2.15.1 by xtable 1.7-0 package
      % Tue Oct 09 11:29:21 2012
      \begin{table}[ht]
      \begin{center}
      \begin{tabular}{llllll}
        \hline
            & Ys & 1 & 2 & 3 & 4 \\ 
        Xs &      &     &     &     &     \\ 
         \hline
      1  &      &   0 &   0 &   0 &   1 \\ 
        2  &      &   0 &   1 &   0 &   0 \\ 
        3  &      &   1 &   0 &   0 &   0 \\ 
        4  &      &   0 &   0 &   1 &   0 \\ 
         \hline
      \end{tabular}
      \end{center}
      \end{table}
      

      【讨论】:

      • 太棒了。可惜 R 让打印一个简单的表格变得如此困难。
      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2013-06-09
      • 2017-11-09
      相关资源
      最近更新 更多