组间 Gsummary 拆分 add_n
Gtsummary split add_n between groups
示例数据
library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)
trial2 %>%
tbl_summary(by = trt) %>%
add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
add_overall() %>%
add_n() %>%
modify_header(label ~ "**Variable**") %>%
modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
modify_footnote(
all_stat_cols() ~ "Median (IQR) or Frequency (%)"
) %>%
modify_caption("**Table 1. Patient Characteristics**") %>%
bold_labels()
输出提供了这个
如何按组拆分为每个变量列出的 N,目前指的是可用于整个队列的 n,这样列就像
变量名,N's available for group 1,Group 1,N's available for group 2,p value
您可以使用 add_stat()
函数添加自定义统计信息。示例如下!
library(gtsummary)
add_by_n <- function(data, variable, by, tbl, ...) {
data %>%
select(all_of(c(variable, by))) %>%
dplyr::group_by(.data[[by]]) %>%
dplyr::summarise_all(~sum(!is.na(.))) %>%
rlang::set_names(c("by", "variable")) %>%
dplyr::left_join(
tbl$df_by %>% select(by, by_col),
by = "by"
) %>%
mutate(
by_col = paste0("add_n_", by_col),
variable = style_number(variable)
) %>%
select(-by) %>%
tidyr::pivot_wider(names_from = by_col,
values_from = variable)
}
tbl <-
trial %>%
select(trt, age, grade) %>%
tbl_summary(by = trt) %>%
add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
add_overall() %>%
add_n() %>%
add_stat(
fns = everything() ~ add_by_n
) %>%
modify_header(starts_with("add_n_stat") ~ "**N**") %>%
modify_table_body(
~ .x %>%
dplyr::relocate(add_n_stat_1, .before = stat_1) %>%
dplyr::relocate(add_n_stat_2, .before = stat_2)
) %>%
modify_spanning_header(
c(all_stat_cols(F), starts_with("add_n_stat")) ~ "**Treatment Received**"
)
由 reprex package (v2.0.0)
于 2021-08-06 创建
示例数据
library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)
trial2 %>%
tbl_summary(by = trt) %>%
add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
add_overall() %>%
add_n() %>%
modify_header(label ~ "**Variable**") %>%
modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
modify_footnote(
all_stat_cols() ~ "Median (IQR) or Frequency (%)"
) %>%
modify_caption("**Table 1. Patient Characteristics**") %>%
bold_labels()
输出提供了这个
如何按组拆分为每个变量列出的 N,目前指的是可用于整个队列的 n,这样列就像 变量名,N's available for group 1,Group 1,N's available for group 2,p value
您可以使用 add_stat()
函数添加自定义统计信息。示例如下!
library(gtsummary)
add_by_n <- function(data, variable, by, tbl, ...) {
data %>%
select(all_of(c(variable, by))) %>%
dplyr::group_by(.data[[by]]) %>%
dplyr::summarise_all(~sum(!is.na(.))) %>%
rlang::set_names(c("by", "variable")) %>%
dplyr::left_join(
tbl$df_by %>% select(by, by_col),
by = "by"
) %>%
mutate(
by_col = paste0("add_n_", by_col),
variable = style_number(variable)
) %>%
select(-by) %>%
tidyr::pivot_wider(names_from = by_col,
values_from = variable)
}
tbl <-
trial %>%
select(trt, age, grade) %>%
tbl_summary(by = trt) %>%
add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
add_overall() %>%
add_n() %>%
add_stat(
fns = everything() ~ add_by_n
) %>%
modify_header(starts_with("add_n_stat") ~ "**N**") %>%
modify_table_body(
~ .x %>%
dplyr::relocate(add_n_stat_1, .before = stat_1) %>%
dplyr::relocate(add_n_stat_2, .before = stat_2)
) %>%
modify_spanning_header(
c(all_stat_cols(F), starts_with("add_n_stat")) ~ "**Treatment Received**"
)