【问题标题】:gtsummary modified cross tabgtsummary 修改后的交叉表
【发布时间】:2021-07-10 21:11:16
【问题描述】:

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

我需要帮助编写 gstummary r 代码以生成下表 output.dummy 表,如上表所示

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

library(gtsummary)

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

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

id age sex country edu ln ivds n2 p5
1 a M eng x 45 15 40 15
2 a M eng x 23 26 70 15
4 a M eng x 26 36 35 40
5 b F eng x 26 25 36 47
6 b F wal y 45 45 60 12
7 b M wal y 60 25 36 15
8 c M wal 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")

[

【问题讨论】:

  • 请用dput展示一个可重复的小例子
  • 你试过什么没用?

标签: r stat gtsummary


【解决方案1】:

我制作了一个类似于您上面的表格(更类似于您更新之前的表格)。但我认为它可以帮助你完成大部分工作。

您请求的表格类型正在开发中。同时,您将需要使用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 创建

【讨论】:

  • 非常简单的问题,有没有办法可以下载 bstfun 的存档文件 (library(bstfun) # install with remotes::install_github("ddsjoberg/bstfun")) 。实际上我是在没有互联网的电脑上做这个分析的。获得文件后,我将通过 USB 传输,然后通过存档/zip 文件夹进行安装。
  • 当我尝试在不同的数据集上运行时。我收到以下错误 tbls_row1 % purrr::map(~tbl_2way_summary(data = trial2, row = age, col = constant, con = all_of(.x ), statistic = "{mean} ({sd}) - {min}, {max}") %>% modify_header(stat_1 = paste0("", .x, "")) ) %>% tbl_merge() %>% modify_spanning_header(everything() ~ NA)
  • 错误:mutate()tbl 有问题。我tbl = %>%(...)。 x mutate()tbl_stats 有问题。我tbl_stats = pmap(...)。 x 汇总类型为“分类”的“肺功能”的汇总统计信息时出错。
  • 此错误有 2 个常见来源。 1. 您已请求汇总统计数据,用于将变量汇总为分类变量的连续变量。要将汇总类型更改为连续,请添加参数type = list(lungfunction ~ 'continuous') 2。statistic= 参数中的函数或统计数据之一无效。运行rlang::last_error() 以查看错误发生的位置。
  • 我用数据集的截图更新了问题,这给了我错误
猜你喜欢
  • 1970-01-01
  • 2013-04-04
  • 2021-05-29
  • 2023-03-29
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多