我正在尝试使用r对标头进行分组。这是我正在使用的输入数据帧。
|usage |flavor |variable |level | mean| se|
|:---------|:------|:------------|:-----|---------:|---------:|
|Every Day |Yes |a |NA | 0.1844236| 0.0645649|
|Every Day |No |a |NA | 0.2074303| 0.0830913|
|Some Days |Yes |a |NA | 0.1748468| 0.0279065|
|Some Days |No |a |NA | 0.2720068| 0.0512438|
我使用以下代码进行分组
kable(data) %>%
kable_styling("striped") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2))
我得到的输出是:
|V1 |V2 |V3 |V4 |
|:--------|:------------|:------------|:------------|:------------|
|usage |Every Day |Every Day |Some Days |Some Days |
|flavor |Yes |No |Yes |No |
|variable |a |a a |a |
|level |NA |NA |NA |NA |
|mean |0.1844236 |0.2074303 |0.1748468 |0.2720068 |
我想要压缩输入数据框的是这样的:
|EVERYDAY |SOMEDAYS
---------------------------------------------------------
|flavor yes |flavor no |flavor yes |flavor no |
|:--------|:------------|:------------|:------------|:------------|
|variable |a |a a |a |
|level |NA |NA |NA |NA |
|mean |0.1844236 |0.2074303 |0.1748468 |0.2720068 |
如何按上述方式完成此分组。
我确实尝试了kableextra,它添加了另一行名称,例如v1 v2 v3 v4。
kable(data, format = "html", caption = "Demo Table") %>%
kable_styling(bootstrap_options = "striped",
full_width = F) %>%
add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
add_footnote(c("table footnote"))