这是this个问题的后续问题
请考虑以下示例:
library(gtsummary)
library(gtreg)
library(dplyr)
library(tidyr)
df <- structure(
list(id = c("patient1", "patient2", "patient3", "patient4",
"patient5"), x = c("h,a,a", "i", "i", "i,a,e", "h")),
class = "data.frame", row.names = c(NA, -5L)
)
df1 <- structure(
list(id = c("patient1", "patient2", "patient3", "patient4",
"patient5"), y = c("yes", "no", "yes", "yes", "no")),
class = "data.frame", row.names = c(NA, -5L)
)
# prep data for a table
tbl <- df |>
separate_rows(x, sep = ",") |>
select(x) |>
count(x) |>
mutate(percent = round(n/nrow(df)*100)) |>
mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") |>
select(x, N = N_percent) |>
mutate(grouping_var = "x", .before = 1L) |>
# create a gtsummary class listing
tbl_listing(df, group_by = grouping_var) |>
# style table with gtusmmary functions to make it look like `tbl_summary()`
modify_header(
x ~ "**Characteristic**",
N ~ "**N = 5**"
) |>
modify_column_alignment(N, align = "center") |>
modify_footnote(N = "n/N (%)")
tbl1 <- df1 |>
select(y) |>
tbl_summary(
type = list(y ~ "categorical"))
tbl_stack(
tbls = list(tbl, tbl1)
)
结果是移动了第二个表:
我预计会是这样的: