有没有办法在 gtsummary 中将两列折叠为一列?
Is there a way to collapse two columns to one in gtsummary?
我正在尝试折叠成列,以便拥有一个列以适应给定的 table 结构。所需的输出为 23 (21.2 - 24.5)。我已经创建了两个 table,但我似乎不明白。本质上,我希望比例和 CI 位于只有一个标题的单个列中 % (95% CI)
library(gtsummary)
library(tidyverse)
# Obtain proportions
tbl1 <-
trial %>%
mutate(trt = as.factor(trt)) %>%
select(grade, response, trt) %>%
tbl_summary(missing = "no", type = everything() ~ "categorical",
statistic = all_categorical() ~ "{p}")
myci2 <-
tbl1$meta_data %>%
filter(summary_type %in% c("categorical", "dichotomous")) %>%
select(summary_type, var_label, df_stats) %>%
unnest(df_stats) %>%
mutate(
conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
style_percent(symbol = TRUE),
conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
style_percent(symbol = TRUE),
ci = str_glue("{conf.low}, {conf.high}"),
label = coalesce(variable_levels, var_label),
row_type = ifelse(summary_type == "dichotomous", "label", "level")
) %>%
select(variable, row_type, label, ci)
finaltbl <-
tbl1 %>%
modify_table_body(
left_join,
myci2,
by = c("variable", "row_type", "label")
) %>%
modify_header(ci = "**% (95% CI)**")
finaltbl
finaltbl[["table_body"]] <- finaltbl[["table_body"]] %>%
mutate(prev = paste0(stat_0, " (", ci, ")"))
finaltbl %>%
modify_column_unhide(prev) %>%
modify_column_hide(c(stat_0, ci))
示例如下!
library(gtsummary)
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.1
packageVersion("gtsummary")
#> [1] '1.4.2.9008'
# Obtain proportions
tbl1 <-
trial %>%
mutate(trt = as.factor(trt)) %>%
select(grade, response, trt) %>%
tbl_summary(missing = "no", type = everything() ~ "categorical",
statistic = all_categorical() ~ "{p}%")
myci2 <-
tbl1$meta_data %>%
filter(summary_type %in% c("categorical", "dichotomous")) %>%
select(summary_type, var_label, df_stats) %>%
unnest(df_stats) %>%
mutate(
conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
style_percent(symbol = TRUE),
conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
style_percent(symbol = TRUE),
ci = str_glue("{style_percent(p)}% ({conf.low}, {conf.high})"),
label = coalesce(variable_levels, var_label),
row_type = ifelse(summary_type == "dichotomous", "label", "level")
) %>%
select(variable, row_type, label, ci)
finaltbl <-
tbl1 %>%
modify_table_body(
left_join,
myci2,
by = c("variable", "row_type", "label")
) %>%
modify_header(ci = "**% (95% CI)**") %>%
modify_column_hide(stat_0)