将多个汇总表与子标题组合在一起

时间:2017-04-14 13:25:36

标签: r r-markdown

我有几个人口统计因素的数据。

我正在尝试创建这样的发布质量汇总表:

                     N  
Sex
      M             150
      F             150
Marital Status
      Single        100
      Married       100
      Divorced      100
Age
      <25           75
      25-34         75
      35-44         75
      >= 45         75

我可以轻松生成每个单独的部分,如下所示:

    require(dplyr)

 dd <- data.frame(barcode = c("16929", "64605", "03086", "29356", "23871"),
   sex = factor(c("M", "F", "M", "F", "M")), 
   marital = factor(c("Married", "Single", "Single", "Single", "Divorced")),
   age_group = factor(c("<25", "25-34", "35-44", "45-54", ">= 55")))

require(dplyr)

age_groups <- dd %>% group_by(age_group) %>% count()
sex <- dd %>% group_by(sex) %>% count()
marital <- dd %>% group_by(marital) %>% count()

我可以使用kablepander中的任何一种解决方案为每个解决方案创建单独的RMarkdown表。

require(knitr)
kable(age_groups)
kable(sex)
kable(marital)

但我无法找到一种方法将它们组合为单个表格的一部分,并为每个类别添加副标题。单独的表具有不同的列宽并手动对齐它们并插入插入的副标题行(在原始LaTeX?中)似乎是一个糟糕的解决方案。

这是一种非常常见的报告格式 - 许多期刊文章的标准表1 - 我想找到创建它的一般解决方案。

3 个答案:

答案 0 :(得分:3)

library(expss)
library(knitr)
dd = data.frame(barcode = c("16929", "64605", "03086", "29356", "23871"),
                 sex = factor(c("M", "F", "M", "F", "M")), 
                 marital = factor(c("Married", "Single", "Single", "Single", "Divorced")),
                 age_group = factor(c("<25", "25-34", "35-44", "45-54", ">= 55"), 
                                    levels = c("<25", "25-34", "35-44", "45-54", ">= 55")))


dd %>% tab_cells("Sex" = sex, "Marital status" = marital, "Age" = age_group) %>% 
    tab_cols(total(label = "N")) %>% 
    tab_stat_cases(total_row_position = "none") %>% 
    tab_pivot()

上面的代码生成输出,如示例所示,但仅适用于HTML输出。 第二个代码段适用于所有knitr格式,但输出与您的示例略有不同。

dd %>% tab_cells("Sex" = sex, "Marital status" = marital, "Age" = age_group) %>% 
    tab_cols(total(label = "N")) %>% 
    tab_stat_cases(total_row_position = "none") %>% 
    tab_pivot() %>% 
    split_columns() %>% 
    kable()

首先输出:

第二次输出:

答案 1 :(得分:0)

这不是一个完整的解决方案,但一个好的起点可能是pander包。

library(pander)
pander(list(`Age groups` = age_groups, `Sex` = sex, `Marital` = marital))
# 
# 
#   * **Age groups**:
# 
#     ---------------
#      age_group   n
#     ----------- ---
#         <25      1
# 
#        >= 55     1
# 
#        25-34     1
# 
#        35-44     1
# 
#        45-54     1
#     ---------------
# 
#   * **Sex**:
# 
#     ---------
#      sex   n
#     ----- ---
#       F    2
# 
#       M    3
#     ---------
# 
#   * **Marital**:
# 
#     --------------
#      marital    n
#     ---------- ---
#      Divorced   1
# 
#      Married    1
# 
#       Single    3
#     --------------
# 
# 
# <!-- end of list -->
# 
# 

答案 2 :(得分:0)

我遇到了同样的问题,在摆弄带有组标签的空行之后,我偶然发现了kableExtra :: group_rows选项。

通过这种方式,您可以将表中的某些行分组并为其添加标签。

在这里添加这些答案是因为三年后我发现了这个问题,也许有人可以从中受益。

require(dplyr)

dd <- data.frame(barcode = c("16929", "64605", "03086", "29356", "23871"),
                 sex = factor(c("M", "F", "M", "F", "M")), 
                 marital = factor(c("Married", "Single", "Single", "Single", "Divorced")),
                 age_group = factor(c("<25", "25-34", "35-44", "45-54", ">= 55")))

require(dplyr)

age_groups <- dd %>%
  group_by(age_group) %>%
  count() %>% 
  rename(variable = age_group) %>% 
  ungroup

sex <- dd %>% 
  group_by(sex) %>% 
  count() %>% 
  rename(variable = sex)%>% 
  ungroup

marital <- dd %>% 
  group_by(marital) %>% 
  count() %>% 
  rename(variable = marital)%>% 
  ungroup

freq_table <- age_groups %>% 
  add_row(sex) %>% 
  add_row(marital)

knitr::kable(freq_table) %>% 
  kableExtra::group_rows(group_label = "Age groups", start_row = 1, end_row = 5) %>% 
  kableExtra::group_rows(group_label = "Sex", start_row = 6, end_row = 7) %>% 
  kableExtra::group_rows(group_label = "Marital status", start_row = 8, end_row = 10)

Short table with group labels

不幸的是,由于我的信誉不足,因此无法在此处直接添加图片。