有没有办法将百分比添加到 tbl_regression add_nevent?
Is there a way to add percentage to tbl_regression add_nevent?
我刚刚发现 gtsummary 中的 add_nevent 可以有选项 location = "level"。我全神贯注了!但我希望它也有一个百分比。我尝试添加 statistic = "{n}({p}%)" 但没有任何变化。
这是我的代码:
tbl_regression(glm(rellife ~ age + gender, data = df, family = "binomial"), exponentiate = TRUE) %>%
add_nevent(location = "level", statistic = "{n}/{N}%") %>% # add number of events of the outcome
add_n(location = "level")
以及 table:
我希望在事件 N 列中有 1601 (93.6%) 的年龄等等。
如有任何帮助,我们将不胜感激。
谢谢
添加N和N事件后,可以使用modify_table_body()
函数计算事件率。示例如下!
library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.5.2'
tbl <-
glm(response ~ age + grade, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE) %>%
add_nevent(location = "level") %>%
add_n(location = "level") %>%
# adding event rate
modify_table_body(
~ .x %>%
dplyr::mutate(
stat_nevent_rate =
ifelse(
!is.na(stat_nevent),
paste0(style_sigfig(stat_nevent / stat_n, scale = 100), "%"),
NA
),
.after = stat_nevent
)
) %>%
# merge the colums into a single column
modify_cols_merge(
pattern = "{stat_nevent} / {stat_n} ({stat_nevent_rate})",
rows = !is.na(stat_nevent)
) %>%
# update header to event rate
modify_header(stat_nevent = "**Event Rate**")
由 reprex package (v2.0.1)
创建于 2022-03-21
我刚刚发现 gtsummary 中的 add_nevent 可以有选项 location = "level"。我全神贯注了!但我希望它也有一个百分比。我尝试添加 statistic = "{n}({p}%)" 但没有任何变化。
这是我的代码:
tbl_regression(glm(rellife ~ age + gender, data = df, family = "binomial"), exponentiate = TRUE) %>%
add_nevent(location = "level", statistic = "{n}/{N}%") %>% # add number of events of the outcome
add_n(location = "level")
以及 table:
我希望在事件 N 列中有 1601 (93.6%) 的年龄等等。
如有任何帮助,我们将不胜感激。 谢谢
添加N和N事件后,可以使用modify_table_body()
函数计算事件率。示例如下!
library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.5.2'
tbl <-
glm(response ~ age + grade, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE) %>%
add_nevent(location = "level") %>%
add_n(location = "level") %>%
# adding event rate
modify_table_body(
~ .x %>%
dplyr::mutate(
stat_nevent_rate =
ifelse(
!is.na(stat_nevent),
paste0(style_sigfig(stat_nevent / stat_n, scale = 100), "%"),
NA
),
.after = stat_nevent
)
) %>%
# merge the colums into a single column
modify_cols_merge(
pattern = "{stat_nevent} / {stat_n} ({stat_nevent_rate})",
rows = !is.na(stat_nevent)
) %>%
# update header to event rate
modify_header(stat_nevent = "**Event Rate**")