是否可以转置 tbl_summary table (Pivot table)

Is it possible to transpose tbl_summary table (Pivot table)

我想使用 tbl_summary

使用数据框创建一个枢轴 table
head(tblbdcc)
# A tibble: 6 x 11
  ntdcc    miphlycc anopmcrpcc anomcrtcc cleftscc impanuscc hypcc talipcc limbcc omphcc gascc
  <fct>    <fct>     <dbl+lbl> <fct>     <fct>    <fct>     <fct> <fct>   <fct>  <fct>  <fct>
1 Cases    NA               NA NA        NA       NA        NA    NA      NA     NA     NA   
2 NA       Cases            NA NA        NA       NA        NA    NA      NA     NA     NA   
3 Controls NA               NA NA        NA       NA        NA    NA      NA     NA     NA   
4 Controls NA               NA NA        NA       NA        NA    NA      NA     NA     NA   
5 Controls NA               NA NA        NA       NA        NA    NA      NA     NA     NA   
6 NA       NA               NA NA        NA       NA        Cases NA      NA     NA     NA

tblbdscc <- tblbdcc %>%
     tbl_summary( 
      missing = "no",
      statistic = all_categorical() ~ "{n}"
)

我想将 table 从

gtsummary 中没有主元 table,但您可以一次构造 table 一行并将结果 table 堆叠起来。示例如下。

library(gtsummary)
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.2
packageVersion("gtsummary")
#> [1] '1.5.0'
set.seed(11234)

# simulate data
df <- 
  tibble(
    ntdcc = sample(c("Case", "Control", NA), 20, replace = TRUE),
    gascc = sample(c("Case", "Control", NA), 20, replace = TRUE),
    limbcc = sample(c("Case", "Control", NA), 20, replace = TRUE)
  )

# loop over every column in df to construct table
tbl <- 
  names(df) %>%
  map(
    # build summary table for single category
    ~ df %>%
      select(all_of(.x)) %>%
      mutate(..true.. = TRUE) %>%
      filter(complete.cases(.)) %>%
      tbl_summary(
        by = all_of(.x),
        missing = "no",
        statistic = ~"{n}", 
        label = list(..true.. = .x)
      ) %>%
      # update column headers
      modify_header(
        all_stat_cols() ~ "**{level}**",
        label = "**Category Assigned**"
      )
  ) %>%
  # stack all tbls together
  tbl_stack() %>%
  # remove all footnotes
  modify_footnote(all_stat_cols() ~ NA)

reprex package (v2.0.1)

创建于 2022-01-19