添加标签以对生成的 gtsummary 中的相似变量进行分组 table

Add labels to group similar variables in a gtsummary generated table

如何在生成的 gtsummary 中添加标签以对相似变量进行分组 table?例如,如果调查受访者能够 select 多个种族(在单独的变量中捕获),我想要种族变量的标题:

这是 tbl_summary 输出:

library(tidyverse)
library(gtsummary)
library(knitr)
tibble::tribble(
  ~race___1, ~race___2, ~race___3, ~race___4, ~race___5,
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",     "Yes",      "No",      "No",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes",
       "No",      "No",      "No",      "No",     "Yes"
  ) %>% tbl_summary() %>% as_kable()
Characteristic N = 10
race___1
No 10 (100%)
race___2
No 10 (100%)
race___3 1 (10%)
race___4
No 10 (100%)
race___5 9 (90%)

reprex package (v2.0.1)

创建于 2022-03-11

您需要使用 bstfun::add_variable_grouping() 函数 (https://www.danieldsjoberg.com/bstfun/reference/add_variable_grouping.html)。

示例如下!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

tbl <-
  tibble::tribble(
    ~race___1, ~race___2, ~race___3, ~race___4, ~race___5,
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",     "Yes",      "No",      "No",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes",
    "No",      "No",      "No",      "No",     "Yes"
  ) %>% 
  tbl_summary(value = everything() ~ "Yes") %>%
  bstfun::add_variable_grouping(
    "Race" = c("race___1", "race___2", "race___3", "race___4", "race___5")
  )

reprex package (v2.0.1)

创建于 2022-03-11