gtsummary - 多个变量的宽格式汇总表

问题描述 投票:0回答:1

有没有办法在

dataframe
包中生成具有多个连续变量(宽格式)的
gtsummary
的汇总表?因此,我想获得一个包含原始汇总统计信息的表格(如多行显示),每个变量的宽格式与
dataframe
中相同。

我尝试使用

tbl_summary
函数,但输出中的所有变量都以长格式排列。

我想要的决赛桌如图所示。 Sample three variables with summary statistics

variables output gtsummary
1个回答
0
投票

这是一个如何完成这项工作的示例

library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.7.2'

# vector of column names to summarize
c("age", "marker") |> 
  lapply(
    \(x) {
      trial |> 
        # rename and select variable
        select(var = all_of(x)) |> 
        tbl_summary(
          type = ~"continuous2",
          statistic = ~c("{mean} ({sd})", "{median}"),
          label = ~"generic_label",
          missing = "no"
        ) |> 
        # update header to indcate the variable
        modify_header(stat_0 = glue::glue("**{x}**")) |> 
        # remove the header row
        remove_row_type(type = "header") |> 
        # remove the default indentation
        modify_table_styling(columns = label, text_format = "indent", undo_text_format = TRUE)
    }
  ) |> 
  tbl_merge(tab_spanner = FALSE) |> 
  as_kable() # convert to kable to display on stackoverflow
特点 年龄 标记
平均值(SD) 47 (14) 0.92 (0.86)
中位数 47 0.64

创建于 2023-12-26,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.