我想使用tbl_summary()tbl_strata()来创建摘要表:

library(gtsummary)
library(ggplot2)

data("mpg")

mpg |> 
  select(hwy, cyl, manufacturer) |> 
  tbl_strata(strata = manufacturer, ~ .x |> 
               tbl_summary(by = cyl, type = list("hwy" ~ "continuous")),
             .combine_with = "tbl_stack")

enter image description here

但我想省略"hwy"标签,只使用"制造商"作为标签,以消除许多空白行.有可能吗?

推荐答案

这与?tbl_strata帮助文件中的第二个示例非常相似.

  • 您需要使用.combine_args = list(group_header = NULL)来 suppress 默认堆叠标头.
  • 更新为tbl_strata2(),将分层级别作为第二个参数,并添加此行以更新标签label = list(hwy = .y)
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.7.2'

data("mpg", package = "ggplot2")

mpg |>
  select(hwy, cyl, manufacturer) |>
  tbl_strata2(
    strata = manufacturer,
    ~ .x |>
      tbl_summary(
        by = cyl,
        type = list(hwy = "continuous"),
        label = list(hwy = .y)
      ),
    .combine_with = "tbl_stack",
    .combine_args = list(group_header = NULL)
  ) |> 
  as_kable() # convert to kable to display on SO
#> ℹ Column headers among stacked tables differ. Headers from the first table are
#> used. Use `quiet = TRUE` to suppress this message.
Characteristic 4, N = 8 6, N = 9 8, N = 1
audi 29 (27, 29) 25 (25, 26) 23 (23, 23)
chevrolet 29 (28, 29) 26 (26, 28) 20 (17, 24)
dodge 24 (24, 24) 22 (18, 23) 16 (15, 17)
ford 19 (17, 25) 17 (17, 21)
honda 32 (32, 34)
hyundai 28 (27, 29) 25 (24, 26)
jeep 20 (20, 21) 17 (14, 18)
land rover 17 (15, 18)
lincoln 17 (17, 18)
mercury 18 (18, 19) 18 (18, 19)
nissan 30 (29, 31) 25 (19, 26) 18 (18, 18)
pontiac 27 (26, 27) 25 (25, 25)
subaru 26 (25, 26)
toyota 30 (23, 31) 20 (19, 26) 17 (16, 18)
volkswagen 29 (29, 29) 29 (29, 29) 25 (24, 26)

创建于2024年4月12日,共有reprex v2.1.0

R相关问答推荐

在R中使用自定义函数时如何删除该函数的一部分?

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

根据列表中项目的名称多次合并数据框和列表

在R中查找每个组不同时间段的总天数

在"gt"表中添加第二个"groupname_col",而不连接列值

移除仪表板Quarto中顶盖和车身之间的白色区域

如何使用ggplot对堆叠条形图进行嵌套排序?

在RStudio中堆叠条形图和折线图

根据类别合并(汇总)某些行

Geom_Hline将不会出现,而它以前出现了

按时间顺序对不同事件进行分组

如何在PrePlot()中将多个元素设置为斜体

循环遍历多个变量,并将每个变量插入函数R

按组和连续id计算日期差

R仅当存在列时才发生变异

如何在内联代码中添加额外的空格(R Markdown)

在直方图中显示两个变量

为什么在POSIXct-times的向量上循环会改变R中的类型?

通过分析特定列中的字符串在数据框中创建新的行和列

Data.table条件合并