您需要将 gtsummary 表转换为 gt 表。然后你可以使用gt::tab_spanner()
放置更高级别的跨越标题。
library(dplyr)
library(gtsummary)
library(purrr)
set.seed(92922)
df <- tibble(y_1980 = rbinom(n = 10, size = 1, prob = .4),
y_1990 = rbinom(n = 10, size = 1, prob = .7),
x1 = rnorm(10, sd = 1),
x2 = rnorm(10, sd = 2))
tbls <- c("y_1980 ~ x1", "y_1980 ~ x1 + x2", "y_1990 ~ x1", "y_1990 ~ x1 + x2") %>%
map(as.formula) %>%
map(glm,
data = df,
family = binomial(link = "logit")) %>%
map(tbl_regression, exponentiate = TRUE) %>%
map(add_significance_stars, hide_ci = TRUE, hide_p = TRUE, hide_se = FALSE) %>%
map(add_glance_table, include = nobs)
tbl <-
tbls %>%
tbl_merge(tab_spanner = FALSE) %>%
modify_table_body(~.x %>% dplyr::arrange(row_type == "glance_statistic"))
show_header_names(tbl)
gt_tbl <-
as_gt(tbl) %>%
gt::tab_spanner(
columns = c(estimate_1, std.error_1, estimate_3, std.error_3),
label = "(1)",
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_2, std.error_2, estimate_4, std.error_4),
label = "(2)",
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_1, std.error_1, estimate_2, std.error_2),
label = "1980",
level = 2,
gather = FALSE
) %>%
gt::tab_spanner(
columns = c(estimate_3, std.error_3, estimate_4, std.error_4),
label = "1990",
level = 2,
gather = FALSE
)