【发布时间】:2021-09-17 12:24:46
【问题描述】:
我正在尝试折叠到列以便拥有单个列以适应给定的表结构。所需的输出为 23 (21.2 - 24.5)。我已经创建了两个表,但我似乎没有得到它。本质上,我希望比例和 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))
【问题讨论】:
-
你为什么不把数据传递给
gtsummary之前的两列dplyr::unite? -
我正在尝试,但似乎得到了错误
-
我正在尝试修改表格正文,但似乎无法正常工作。让我检查 unite(stat_0, ci) 中的错误:找不到对象“stat_0”
-
stat_0来自哪里?您应该在将数据传递给gtsummary之前对其进行操作。我认为您已经在trial数据框(如果那是一个数据框)中获得了您所需要的一切,请将其发布,以便我们查看其中的内容并提出解决方案。