在 GTSummary 中对行进行分组

Grouping Rows in GTSummary

我正在尝试对一些 rows/variables(分类的和连续的)进行分组,以帮助提高大型数据集中的 table 可读性。

这是虚拟数据集:

library(gtsummary)
library(tidyverse)
library(gt)
set.seed(11012021)

# Create Dataset
PIR <- 
  tibble(
    siteidn = sample(c("1324", "1329", "1333", "1334"), 5000, replace = TRUE, prob = c(0.2, 0.45, 0.15, 0.2)) %>% factor(),
    countryname = sample(c("NZ", "Australia"), 5000, replace = TRUE, prob = c(0.3, 0.7)) %>% factor(),
    hospt = sample(c("Metropolitan", "Rural"), 5000, replace = TRUE, prob = c(0.65, 0.35)) %>% factor(),
    age = rnorm(5000, mean = 60, sd = 20),
    apache2 = rnorm(5000, mean = 18.5, sd=10),
    apache3 = rnorm(5000, mean = 55, sd=20),
    mechvent = sample(c("Yes", "No"), 5000, replace = TRUE, prob = c(0.4, 0.6)) %>% factor(),
    sex = sample(c("Female", "Male"), 5000, replace = TRUE) %>% factor(),
    patient = TRUE
  ) %>%
  mutate(patient_id = row_number())%>% 
  group_by(
    siteidn) %>% mutate(
      count_site = row_number() == 1L) %>%
  ungroup()%>% 
  group_by(
    patient_id) %>% mutate(
      count_pt = row_number() == 1L) %>%
  ungroup()

然后我使用下面的代码生成我的table:

t1 <- PIR %>% 
  select(patientn = count_pt, siten = count_site, age, sex, apache2, apache3,  apache2, mechvent, countryname) %>% 
  tbl_summary(
    by = countryname,
    missing = "no", 
    statistic = list(
      patientn ~ "{n}",
      siten ~ "{n}",
      age ~ "{mean} ({sd})",
      apache2 ~ "{mean} ({sd})",
      mechvent ~ "{n} ({p}%)",
      sex ~ "{n} ({p}%)",
      apache3 ~ "{mean} ({sd})"),
    label = list(
      siten = "Number of ICUs",
      patientn = "Number of Patients",
      age = "Age",
      apache2 = "APACHE II Score",
      mechvent = "Mechanical Ventilation",
      sex = "Sex",
      apache3 = "APACHE III Score")) %>% 
  modify_header(stat_by = "**{level}**") %>%
  add_overall(col_label = "**Overall**")
  
t2 <- PIR %>% 
  select(patientn = count_pt, siten = count_site, age, sex, apache2, apache3,  apache2, mechvent, hospt) %>% 
  tbl_summary(
    by = hospt,
    missing = "no", 
     statistic = list(
      patientn ~ "{n}",
      siten ~ "{n}",
      age ~ "{mean} ({sd})",
      apache2 ~ "{mean} ({sd})",
      mechvent ~ "{n} ({p}%)",
      sex ~ "{n} ({p}%)",
      apache3 ~ "{mean} ({sd})"),
    label = list(
      siten = "Number of ICUs",
      patientn = "Number of Patients",
      age = "Age",
      apache2 = "APACHE II Score",
      mechvent = "Mechanical Ventilation",
      sex = "Sex",
      apache3 = "APACHE III Score")) %>%  
  modify_header(stat_by = "**{level}**")

tbl <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Country**", "**Hospital Type**")
  ) %>%
  modify_spanning_header(stat_0_1 ~ NA) %>%
  modify_footnote(everything() ~ NA)

这会产生以下结果 table:

我想将某些行组合在一起以便于阅读。理想情况下,我希望 table 看起来像这样:

我尝试使用 gt 包,代码如下:

tbl <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Country**", "**Hospital Type**")
  ) %>%
  modify_spanning_header(stat_0_1 ~ NA) %>%
  modify_footnote(everything() ~ NA) %>% 
as_gt() %>%  
  gt::tab_row_group(
    group = "Severity of Illness Scores",
    rows = 7:8) %>%  
  gt::tab_row_group(
    group = "Patient Demographics",
    rows = 3:6) %>%  
  gt::tab_row_group(
    group = "Numbers",
    rows = 1:2)

这会生成所需的 table:

我在执行此操作时遇到了一些问题。

  1. 当我尝试使用行名称(变量)时,会出现一条错误消息(无法对不存在的列进行子集...)。有没有办法通过使用变量名来做到这一点?对于较大的 tables,我在使用分配行名称的行号方法时遇到了一些麻烦。当有一个变量在移动到末尾以说明分组行时丢失了它的位置时尤其如此。

  2. 有没有办法在进入 tbl_summary 之前执行此操作?虽然我喜欢这个 table 的输出,但我使用 Word 作为统计报告的输出文档,并且希望能够在需要时(或由我的合作者)在 Word 中格式化 tables ).我通常使用 gtsummary::as_flextable 作为 table 输出。

再次感谢,

  1. When I try to use the row names (variables), an error message comes up (Can't subset columns that don't exist...). Is there a way to do this by using the variable names? With larger tables, I am getting into some trouble with using the row numbers method of assigning row names. This is particularly true when there is a single variable that loses its place as it's moved to the end to account for the grouped rows.

有两种方法可以解决此问题,1. 为每个组构建单独的 table,然后堆叠它们,以及 2. 向 .$table_body 添加一个分组列,然后将小标题分组新变量。

library(gtsummary)
library(dplyr)
packageVersion("gtsummary")
#> '1.3.6'

# Method 1 - Stack separate tables
t1 <- trial %>% select(age) %>% tbl_summary()
t2 <- trial %>% select(grade) %>% tbl_summary()

tbl1 <-
  tbl_stack(
    list(t1, t2), 
    group_header = c("Demographics", "Tumor Characteristics")
  ) %>%
  modify_footnote(all_stat_cols() ~ NA)

# Method 2 - build a grouping variable
tbl2 <-
  trial %>%
  select(age, grade) %>%
  tbl_summary() %>%
  modify_table_body(
    mutate,
    group_variable = case_when(variable == "age" ~ "Deomgraphics",
                               variable == "grade" ~ "Tumor Characteristics")
  ) %>%
  modify_table_body(group_by, group_variable)

2.Is there a way to do this prior to piping into tbl_summary? Although I like the output of this table, I use Word as my output document for statistical reports and would like the ability to be able to format the tables in Word if need be (or by my collaborators). I usually use gtsummary::as_flextable for table output.

上面的示例在导出为 gt 格式之前修改了 table,因此您可以将这些示例导出为 flextable。但是,flextable 没有相同的 built-in header 行功能(或者至少我不知道,并且不要在 as_flex_table() 中使用它),并且输出看起来像下面的 table。我建议从 GitHub 安装 gt 的开发版本并导出为 RTF(受 Word 支持)——他们在过去几个月对 RTF 输出做了很多更新,它可能对你有用。