如何将gtsummary表格中的置信区间括号()替换为[]
解决gtsummary置信区间方括号替换问题
要把gtsummary生成的置信区间从默认的()改成[],可以通过以下两种实用方式实现:
方法一:自定义置信区间格式化函数(推荐)
先编写一个自定义格式化函数,复刻fmt_ci()的逻辑但将括号替换为方括号,随后在add_ci()中指定使用该函数:
# 自定义方括号格式的置信区间函数 fmt_ci_brackets <- function(x, pattern = "{conf.low}, {conf.high}", ...) { gtsummary::style_sigfig(x) %>% purrr::pmap_chr(function(conf.low, conf.high, ...) { glue::glue("[{pattern}]", conf.low = conf.low, conf.high = conf.high) }) } # 应用到你的分析代码中 trial %>% select(response, grade) %>% tbl_summary(statistic = all_categorical() ~ "{p}%", missing = "no") %>% modify_footnote(everything() ~ NA) %>% add_ci(fmt_fun = fmt_ci_brackets)
方法二:直接替换字符串
若不想编写自定义函数,也可在生成表格后,用modify_fmt_fun()直接替换括号内容:
trial %>% select(response, grade) %>% tbl_summary(statistic = all_categorical() ~ "{p}%", missing = "no") %>% modify_footnote(everything() ~ NA) %>% add_ci() %>% # 替换左括号为左方括号 modify_fmt_fun(columns = ci, fmt_fun = function(x) stringr::str_replace_all(x, "\\(", "[")) %>% # 替换右括号为右方括号 modify_fmt_fun(columns = ci, fmt_fun = function(x) stringr::str_replace_all(x, "\\)", "]"))
两种方法都能达成需求,方法一更契合gtsummary的格式化逻辑,方法二更简洁直接。
内容的提问来源于stack exchange,提问作者Sajin Kang
相关产品推荐
相关产品推荐

