我试图汇总一个变量的计数数据,该变量按第二个变量以长(非宽)格式分组。我想以编程方式根据第二个变量插入行标题或tab_row_group()
,该变量使用gt
或gtsummary
对主变量进行分类。
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
)
1条答案
按热度按时间plicqrtu1#
可以先将系统器官类变量合并到表体中,再设置到groupname列中。
首先,为系统器官类创建查找表:
然后创建主
tbl_summary
表:最后将系统器官类合并到表体,并使其成为分组变量:
需要
replace_na
部分来确保变量标签不会被分配给“缺失”组。