R包xtable,如何用R创建多行和多列的latextable

时间:2013-02-23 03:35:52

标签: r latex

我的目标是使用R中的multirow / multicolumn功能创建latextable。 我想要的乳胶漆应该是这样的:

               colLabel  |  colLabel2
                         |
                 a1  a2  |   a3    a4  
 -------------------------------------
             b1  1    2  |   5     6
 rowLabel1               |
             b2  3    4  |   7     8
 --------------------------------------
             b3  9   10  |   13    14
 rowLabel2               |
             b4  11  12  |   15    16
 --------------------------------------

我目前正在使用xtable,但我无法弄清楚如何使用此包创建multirow。 谁能告诉我如何创建这样的表?

提前谢谢

4 个答案:

答案 0 :(得分:11)

您必须更具体地了解您要确切制表的内容,但我想tabular包中的tables函数可能会有所帮助。

以下是根据4个二元因子变量对变量进行制表的示例:

mydf <- data.frame(rowFactor1 = sample(letters[1:2], 100, replace = TRUE), 
                colFactor1 = sample(LETTERS[1:2], 100, replace = TRUE), 
                x = rnorm(100), 
                rowFactor2 = sample(1:2, 100, replace = TRUE), 
                colFactor2 = sample(1:2, 100, replace = TRUE))

tab1 <- tabular(Heading()*RowFactor(rowFactor2, spacing = 1, 
                        levelnames = c("rowLabel1", "rowLabel2"))*
                Heading()*RowFactor(rowFactor1, 
                        levelnames = c("b1", "b2")) ~ 
                Heading()*Factor(colFactor2, 
                        levelnames = c("colLabel1", "colLabel2") )*
                Heading()*Factor(colFactor1, 
                        levelnames = c("a1", "a2"))*
                Heading()*(x)*Heading()*(mean), 
        data = mydf)

给你这样的东西,但在使用乳胶输出时很好地形成了

                                                                colLabel1         colLabel2        
                                                                a1        a2      a1        a2     
 \\nopagebreak rowLabel1                       \\nopagebreak b1 -0.1450    0.2633  0.91454   0.1222
                                               \\nopagebreak b2 -0.1499   -0.4290 -0.09706  -0.6977
 \\rule{0pt}{1.7\\normalbaselineskip}rowLabel2 \\nopagebreak b1  0.6976   -0.4888 -0.68492   1.6764
                                               \\nopagebreak b2 -0.2369   -0.1428 -0.66405   0.9469

最后latex(tab1)为您提供了乳胶代码:

\begin{tabular}{llcccc}
\hline
& & \multicolumn{2}{c}{colLabel1} & \multicolumn{2}{c}{colLabel2} \\ 
 &  & a1 & a2 & a1 & \multicolumn{1}{c}{a2} \\ 
\hline
\nopagebreak rowLabel1 & \nopagebreak b1  & $-0.1450$ & $\phantom{-}0.2633$ & $\phantom{-}0.91454$ & $\phantom{-}0.1222$ \\
 & \nopagebreak b2  & $-0.1499$ & $-0.4290$ & $-0.09706$ & $-0.6977$ \\
\rule{0pt}{1.7\normalbaselineskip}rowLabel2 & \nopagebreak b1  & $\phantom{-}0.6976$ & $-0.4888$ & $-0.68492$ & $\phantom{-}1.6764$ \\
 & \nopagebreak b2  & $-0.2369$ & $-0.1428$ & $-0.66405$ & $\phantom{-}0.9469$ \\
\hline 
\end{tabular}

答案 1 :(得分:2)

考虑kableExtra package

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table]{xcolor}
\begin{document}

<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(echo=FALSE)
library(kableExtra)
options(knitr.table.format = "latex")
dat <- data.frame(
  group = c("rowLabel1", "rowLabel1", "rowLabel2", "rowLabel2"),
  a1 = c(1, 3, 9, 11),
  a2 = c(2, 4, 10, 12), 
  a3 = c(5, 7, 13, 15),
  a4 = c(6, 8, 14, 16)
)
@

<<results='asis'>>=
kable(dat, booktabs = TRUE, caption = "My  table", escape = FALSE) %>% 
  add_header_above(c(" ", "colLabel1"=2, "colLabel2"=2)) %>% 
  kable_styling(latex_options = "hold_position") %>%
  column_spec(1, bold=TRUE) %>%
  collapse_rows(columns = 1)
@

\end{document}

enter image description here

答案 2 :(得分:1)

我无法使用列标题,但对于过去的多行值,我已经作弊了。下面的函数会将相同值的第二个和后续集合设置为NA,然后xtable不会显示它们,因此您得到的内容看起来像一个多行值(带有顶部对齐)

cleanf <- function(x){     
    oldx <- c(FALSE, x[-1]==x[-length(x)])  
    # is the value equal to the previous?    
    res <- x
    res[oldx] <- NA
    return(res)} 

答案 3 :(得分:1)

以下是使用var address = 'Uhartia, 64220 Gamarthe, France'; var address = 'Eaux Chaudes Place de l\'Eglise, 64440 Laruns, France'; 包(我是作者)执行此操作的方法:

huxtable

当打印到LaTeX时,它看起来像:

huxtable pic