【问题标题】:xtable header manipulationxtable 标头操作
【发布时间】:2012-06-19 21:10:47
【问题描述】:

我在 R 中使用 xtable 来获取表格的 LaTeX 代码。我想修改“标题”的第一个元素(LaTeX 表中的第一行),但我只看到了一个将文本添加到行尾的选项(使用 xtable 中的 add.to.row)。

下面的例子应该说明我想要什么。

跑步

xtable(matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo'))))

给出输出

% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Tue Jun 19 22:39:49 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & GNU & Leo \\ 
  \hline
absolute & 1.00 & 3.00 \\ 
  relative & 0.25 & 0.75 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

我想接电话

  frequency & GNU & Leo \\ 

而不是行

  & GNU & Leo \\

但我无法使用 xtable 完成此操作。如果您知道如何执行此操作,请告诉我。另外,如果我问错了地方,或者我忽略了答案或明显的解决方案,我提前道歉。

【问题讨论】:

    标签: r latex xtable


    【解决方案1】:

    另一种选择是使用 Hmisc 中的 latex 函数:

    library(Hmisc)
    m <- matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo')))
    latex(m,file = "",rowlabel = "frequency")
    

    【讨论】:

      【解决方案2】:

      一种可能性:

      dat1 <- data.frame(matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo'))))
      dat <- data.frame(Freq = rownames(dat1), dat1)
      
      print(
        xtable(
                 x = dat
               , caption = "Your Caption"
               , label = "tab:dat"
               , align = paste(paste("l|", paste(rep("r", ncol(dat)-1), collapse=''), sep = ""), "|r", sep = "")
               , digits = c(0, rep(3, ncol(dat)))
               )
        , table.placement = "H"
        , caption.placement = "top"
        , include.rownames = FALSE
        , include.colnames = TRUE
        , size = "normalsize"
        , hline.after = c(-1, 0, nrow(dat))
        )
      

      【讨论】:

      • 谢谢!这是一个不错的技巧。
      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多