按第二个变量对gt/gt summary中的行或标题进行分组

mtb9vblg  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(117)

我试图汇总一个变量的计数数据,该变量按第二个变量以长(非宽)格式分组。我想以编程方式根据第二个变量插入行标题或tab_row_group(),该变量使用gtgtsummary对主变量进行分类。

library(gt)
library(gtsummary)
library(tidyverse)

# reprex data

aes <- data.frame(
  preferred_term = c("Headache", "Fatigue", "Anxiety", "Anxiety", "Headache",
                     "Contusion", "Depressed Mood", "Gait disturbance"),
  system_organ_class = c("Nervous System Disorders",
                         "General disorders and administration site conditions",
                         "Psychiatric disorders", "Psychiatric disorders",
                         "Nervous System Disorders", 
                         "Skin and subcutaneous tissue disorders",
                         "Psychiatric disorders",
                         "General disorders and administration site conditions")
)
# manual approach with gt()

left_join(
  aes %>% 
    group_by(preferred_term) %>% 
    summarize(n = n()) %>% 
    mutate(
      p = round((n / sum(n)) * 100, 1),
      n_p = paste0(n, " (", p, ")")
    ),
  aes %>% 
    distinct(),
  by = "preferred_term"
) %>% 
  arrange(system_organ_class) %>% 
  select(-c(n, p, system_organ_class)) %>% 
  gt() %>% 
  tab_row_group(
    label = "General disorders and administration site conditions", rows = 1:2
  ) %>% 
  tab_row_group(
    label = "Nervous System Disorders", rows = 3:3
  ) %>% 
  tab_row_group(
    label = "Psychiatric disorders", rows = 4:5
  ) %>% 
  tab_row_group(
    label = "Skin and subcutaneous tissue disorders", rows = 6:6
  )
# wide summary via gtsummary::tbl_summary()

aes %>%
  tbl_summary(by = system_organ_class)
# manual approach with gtsummary::tbl_summary() and gtsummary::modify_table_body()

aes %>% 
  tbl_summary(
    include = preferred_term
  ) %>%
  modify_table_body(
    mutate,
    groupname_col = case_when(
      label %in% c("Fatigue", "Gait disturbance") ~ "General disorders and administration site conditions",
      label == "Headache" ~ "Nervous System Disorders",
      label %in% c("Anxiety", "Depressed Mood") ~ "Psychiatric disorders",
      label == "Contusion" ~ "Skin and subcutaneous tissue disorders",
      TRUE ~ ""
    )
  )
# hypothetical solution
aes %>% 
  tbl_summary(
    include = preferred_term
  ) %>%
  modify_table_body(
    mutate,
# referring to corresponding class column directly
    groupname_col = .x$inputs$data$system_organ_class
  )
plicqrtu

plicqrtu1#

可以先将系统器官类变量合并到表体中,再设置到groupname列中。
首先,为系统器官类创建查找表:

aes_lookup <- aes %>% 
  select(preferred_term, system_organ_class) %>% 
  unique()

然后创建主tbl_summary表:

aestab <- aes %>%
   tbl_summary(include=preferred_term)

最后将系统器官类合并到表体,并使其成为分组变量:

aestab %>% 
   modify_table_body(~.x %>% 
      left_join(aes_lookup, by=c("label"="preferred_term")) %>% 
      mutate(groupname_col = replace_na(system_organ_class, "")))

需要replace_na部分来确保变量标签不会被分配给“缺失”组。

相关问题