【问题标题】:With gtsummary, is it possible to have N on a separate row to the column name?使用 gtsummary,是否可以将 N 放在列名的单独行上?
【发布时间】:2021-01-28 13:02:43
【问题描述】:

默认情况下,gtsummary 将观察的数量放在该组标签旁边的 by 组中。这增加了表格的宽度……如果有很多组或 N 很大,表格很快就会变得很宽。

是否可以让 gtsummary 在标签下方的单独行上报告 N?例如

> data(mtcars)
> mtcars %>%
+         select(mpg, cyl, vs, am) %>%
+         tbl_summary(by = am) %>%
+         as_tibble()
# A tibble: 6 x 3
  `**Characteristic**` `**0**, N = 19`   `**1**, N = 13`  
  <chr>                <chr>             <chr>            
1 mpg                  17.3 (14.9, 19.2) 22.8 (21.0, 30.4)
2 cyl                  NA                NA               
3 4                    3 (16%)           8 (62%)          
4 6                    4 (21%)           3 (23%)          
5 8                    12 (63%)          2 (15%)          
6 vs                   7 (37%)           7 (54%)          

会变成

# A tibble: 6 x 3
  `**Characteristic**` `**0**`           `**1**`  
  <chr>                <chr>             <chr>            
1                      N = 19            N = 13
2 mpg                  17.3 (14.9, 19.2) 22.8 (21.0, 30.4)
3 cyl                  NA                NA               
4 4                    3 (16%)           8 (62%)          
5 6                    4 (21%)           3 (23%)          
6 8                    12 (63%)          2 (15%)          
7 vs                   7 (37%)           7 (54%)    

(我只使用 as_tibble 以便通过手动编辑很容易显示我的意思...)

有什么想法吗?

谢谢!

【问题讨论】:

    标签: gtsummary


    【解决方案1】:

    这是您可以做到这一点的一种方法:

    library(tidyverse)
    library(gtsummary)
    
    mtcars %>%
      select(mpg, cyl, vs, am) %>%
    # create a new variable to display N in table
      mutate(total = 1) %>%
    # this is just to reorder variables for table 
      select(total, everything()) %>%
      tbl_summary(
        by = am,
    # this is to specify you only want N (and no percentage) for new total variable
        statistic = total ~ "N = {N}") %>%
    # this is a gtsummary function that allows you to edit the header
      modify_header(stat_by = "**{level}**")
    
    • 首先,我正在创建一个新变量,它只是总观察值(称为 total
    • 然后,我将自定义变量统计信息的显示方式
    • 然后我使用gtsummary::modify_header()从标题中删除N

    另外,如果你使用flextableprint engine,你可以在标题本身添加一个换行符:

    mtcars %>%
      select(mpg, cyl, vs, am) %>%
    # create a new variable to display N in table
      tbl_summary(
        by = am
    # this is to specify you only want N (and no percentage) for new total variable
        ) %>%
    # this is a gtsummary function that allows you to edit the header
      modify_header(stat_by =  "{level}\nN = {N}") %>%
      as_flex_table()
    

    祝你好运!

    【讨论】:

    • 确实有效!谢谢!不过感觉有点老套……我希望有一个内置的方法(感觉应该有一个)
    • 添加了使用flextableprint engine在标题本身中放置换行符的选项
    • 这正是我所想的!谢谢@kittykatskat!
    【解决方案2】:

    @kittykatstat 已经发布了两个很棒的解决方案!我将添加一些细微的变化:)

    如果你想使用 {gt} 包来打印表格你输出到 HTML,你可以使用 HTML 标签&lt;br&gt; 在标题行添加一个换行符(非常类似于已经发布的\n 解决方案)。

    library(gtsummary)
    
    mtcars %>%
      select(mpg, cyl, vs, am) %>%
      dplyr::mutate(am = factor(am, labels = c("Manual", "Automatic"))) %>%
      # create a new variable to display N in table
      tbl_summary(by = am) %>%
      # this is a gtsummary function that allows you to edit the header
      modify_header(stat_by =  "**{level}**<br>N = {N}")
    

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 2021-08-05
      • 2023-03-08
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多