R package xtable, how to create a latextable with multiple rows and columns from R

前端 未结 5 686
小鲜肉
小鲜肉 2020-12-13 04:50

My goal is to create latextable with multirow/multicolumn features from R. The latextable I want should look like this:

               colLabel  |  colLabel2         


        
5条回答
  •  情话喂你
    2020-12-13 05:31

    Consider the kableExtra package.

    \documentclass{article}
    \usepackage{booktabs}
    \usepackage{multirow}
    \usepackage[table]{xcolor}
    \begin{document}
    
    <>=
    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)
    )
    @
    
    <>=
    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}
    

提交回复
热议问题