gsummary 修改后的交叉表

gtsummary modified cross tab

[![在此处输入图片描述][2]][2][![我需要帮助编写 gstummary r 代码以生成以下内容 table output.dummy table如上所示table][2]][2]

我需要帮助编写 gstummary r 代码以生成以下 table output.dummy table table

[![在此处输入图片描述][2]][2]

library(gtsummary)

[![在此处输入图片描述][2]][2]

[![在此处输入图片描述][3]][3]

id 年龄 性别 国家 教育 ln ivds n2 p5
1 一个 英语 x 45 15 40 15
2 一个 英语 x 23 26 70 15
4 一个 英语 x 26 36 35 40
5 b F 英语 x 26 25 36 47
6 b F 沃尔 y 45 45 60 12
7 b 沃尔 y 60 25 36 15
8 c 沃尔 y 70 08 25 36
9 c F sco z 80 25 36 15
10 c F sco z 90 25 26 39
structure(list(id = 1:15, age = structure(c(1L, 1L, 2L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 1L, 2L), .Label = c("a", "b", 
"c"), class = "factor"), sex = structure(c(2L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("F", "M"), class = "factor"), 
    country = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 3L), .Label = c("eng", "scot", "wale"
    ), class = "factor"), edu = structure(c(1L, 1L, 1L, 2L, 2L, 
    2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L), .Label = c("x", 
    "y", "z"), class = "factor"), lon = c(45L, 23L, 
    25L, 45L, 70L, 69L, 90L, 50L, 62L, 45L, 23L, 25L, 45L, 70L, 
    69L), is = c(15L, 26L, 36L, 34L, 2L, 4L, 5L, 8L, 9L, 
    15L, 26L, 36L, 34L, 2L, 4L), n2 = c(40L, 70L, 50L, 60L, 
    30L, 25L, 80L, 89L, 10L, 40L, 70L, 50L, 60L, 30L, 25L), p5 = c(15L, 
    20L, 36L, 48L, 25L, 36L, 28L, 15L, 25L, 15L, 20L, 36L, 48L, 
    25L, 36L)), row.names = c(NA, 15L), class = "data.frame")

[

我制作了一个 table 与您上面的类似(更类似于您更新之前的 table)。但我认为它会让你完成大部分工作。

您正在请求的 table 类型正在开发中。同时,您需要使用 bstfun::tbl_2way_summary() 函数。这个函数存在于另一个包中,我们在与 gtsummary 集成之前努力让它变得更好。

library(bstfun) # install with `remotes::install_github("ddsjoberg/bstfun")`
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.1'

# add a column that is all the same value
trial2 <- trial %>% mutate(constant = TRUE)

# loop over each continuous variable, construct table, then merge them together
tbls_row1 <-
  c("age", "marker", "ttdeath") %>%
  purrr::map(
    ~tbl_2way_summary(data = trial2, row = grade, col = constant, con = all_of(.x),
                      statistic = "{mean} ({sd}) - {min}, {max}") %>%
      modify_header(stat_1 = paste0("**", .x, "**")) 
  ) %>%
  tbl_merge() %>%
  modify_spanning_header(everything() ~ NA)

# repeat for the second row
tbls_row2 <-
  c("age", "marker", "ttdeath") %>%
  purrr::map(
    ~tbl_2way_summary(data = trial2, row = stage, col = constant, con = all_of(.x),
                      statistic = "{mean} ({sd}) - {min}, {max}") %>%
      modify_header(stat_1 = paste0("**", .x, "**")) 
  ) %>%
  tbl_merge() %>%
  modify_spanning_header(everything() ~ NA)

# stack these tables
tbl_stacked <- tbl_stack(list(tbls_row1, tbls_row2))

# lastly, add calculated summary stats for categorical variables, and merge them
tbl_summary_stats <-
  trial2 %>%
  tbl_summary(
    include = c(grade, stage),
    missing = "no"
  ) %>%
  modify_header(stat_0 ~ "**n (%)**") %>%
  modify_footnote(everything() ~ NA)

tbl_final <- 
  tbl_merge(list(tbl_summary_stats, tbl_stacked)) %>%
  modify_spanning_header(everything() ~ NA) %>%
  # column spanning column headers
  modify_spanning_header(
    list(c(stat_1_1_2, stat_1_2_2) ~ "**Group 1**",
         stat_1_3_2 ~ "**Group 2**")
  )

reprex package (v2.0.0)

于 2021-07-10 创建