将行跨越列标题添加到编织表表

时间:2019-05-28 09:21:08

标签: r knitr kable kableextra

kableExtra可以添加标题行,其中标题项跨越表中使用kable生成的表中的多列

library(tidyverse)
library(knitr)
library(kableExtra)
mat <- matrix(1:4,2,dimnames=list(c("a","b"),c("x","y")))
mat %>% kable("html") %>% add_header_above(c(" " = 1,"row header" = 2))

table with row header

我也想有一个列标题,就像这样:

table with row and column header

能做到吗?

2 个答案:

答案 0 :(得分:0)

我认为您无法做到,但我找到了解决您问题的方法

mat <- data.frame(a = c("row header","row header"),b = c("a","b"),c = c("x","y"))
mat %>% kable("html") %>% add_header_above(c(" " = 1,"row header" = 2))



kable(mat, align = "c",col.names = c("","","")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12)%>%
column_spec(1, bold = T) %>%
collapse_rows(columns = 1, valign = "middle")%>%
add_header_above(c(" " = 1, "row header" = 2))

我希望这对您有帮助

答案 1 :(得分:0)

基于您告诉我的信息,我能够完全解决我的问题:

mat <- data.frame(a = c("column header","column header"),b = c("a","b"),c = c("x","y"))
kable(mat, align = "c",col.names = c("","","")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12)%>%
column_spec(1, bold = T,width="3em",extra_css="transform: rotate(-90deg);") %>%
collapse_rows(columns = 1, valign = "middle")%>%
add_header_above(c(" " = 1, "row header" = 2))