问题:在SingleChildScrollView.
中,gtsummary
函数计算了全部非缺失观测值中的列百分比。我希望tbl_summary
从缺失和不缺失观测值的总数中计算百分比。
来自http://www.danieldsjoberg.com/gtsummary/articles/gallery.html的gtsummary
表格库中的示例
gtsummary
等级没有遗漏的观察结果,因此,药物A组中有35位患有1级疾病的人是35/98(36%)。
现在,将3年级重新编码为丢失:
trial[c("trt", "age", "grade")] %>%
tbl_summary(
by = trt,
missing = "no",
statistic = all_continuous() ~ "{median} ({p25}, {p75}) [N = {N_nonmiss}]"
) %>%
modify_header(stat_by = md("**{level}**<br>N = {n} ({style_percent(p)}%)")) %>%
add_n() %>%
bold_labels() %>%
modify_spanning_header(starts_with("stat_") ~ "**Chemotherapy Treatment**")
重新运行trial$grade[trial$grade %in% "III"] <- NA
trial$grade <- droplevels(trial$grade)
:
tbl_summary
1级现在表示为n = 35,占67(52%)个不遗漏的obs。在药物A组中。我仍然希望将百分比表示为98个人中的36%。在trial[c("trt", "age", "grade")] %>%
tbl_summary(
by = trt,
missing = "no",
statistic = all_continuous() ~ "{median} ({p25}, {p75}) [N = {N_nonmiss}]"
) %>%
modify_header(stat_by = md("**{level}**<br>N = {n} ({style_percent(p)}%)")) %>%
add_n() %>%
bold_labels() %>%
modify_spanning_header(starts_with("stat_") ~ "**Chemotherapy Treatment**")
中有办法做到这一点吗?
答案 0 :(得分:1)
我认为,获得所需内容的最佳方法是使用forcats::fct_explicit_na()
函数使缺失值成为显式NA。当NA值是一个因子水平时,它将被包含在分母中以进行百分比计算。
library(gtsummary)
library(tidyverse)
trial %>%
select(response, trt) %>%
# make missing value explicit for categorical variables, using fct_explicit_na
mutate(response = factor(response) %>% fct_explicit_na()) %>%
# summarize data
tbl_summary(by = trt)
此解决方案对您有用吗?