0

我试图通过两个分类变量来总结一个连续变量,如下所示。我无法正确地做到这一点。我想知道是否有办法通过gtsummary包装获得这个。谢谢

library("gtsummary")
library("tidyverse")
set.seed(123)
sex <- sample(c("Male", "Female"), size=100, replace=TRUE)
age <- rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
height <- sample(c("Tall", "short"), size=100, replace=TRUE)
bmi <- rnorm(n=100, mean=10 + 4*(sex=="Female") + 2*(height=="Tall"), sd=1)

dsn <- data.frame(sex, age, bmi, height)


tab <- dsn %>% 
  dplyr::select(age, sex) %>% 
  tbl_summary(by = sex) %>% 
  bold_labels() 
tab

 #Characteristic         Female, N = 43           Male, N = 57  
                   ────────────────────────────────────────────────────────────────
                     age              20.00 (19.93, 20.06)   19.99 (19.94, 20.03)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR)                            


tab1 <- dsn %>% 
  filter(height == "Tall") %>% 
  dplyr::select(bmi, sex) %>% 
  tbl_summary(by = sex,
              label = list(bmi ~ "....   Tall"))
tab1

 #Characteristic         Female, N = 22           Male, N = 35  
                   ────────────────────────────────────────────────────────────────
                     ....  Tall       15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR) 

tab2 <- dsn %>% 
  filter(height == "Tall") %>% 
  dplyr::select(bmi, sex) %>% 
  tbl_summary(by = sex,
              label = list(bmi ~ "....   Short"))
tab2

 #Characteristic         Female, N = 22           Male, N = 35  
                   ────────────────────────────────────────────────────────────────
                     ....  Short      15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR)  

# I am trying to obtain the table below
tbl_stack(
  list(tab1, tab2, tab),
  group_header = c("BMI", "", ""))

   #Group   Characteristic         Female, N = 22           Male, N = 35  
               ────────────────────────────────────────────────────────────────────────
                 BMI     ....  Tall       15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                         ....  Short      15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                         age              20.00 (19.93, 20.06)   19.99 (19.94, 20.03)  
               ────────────────────────────────────────────────────────────────────────
                 Statistics presented: median (IQR)  

#Is there an easy way to do this using the gtsummary package

4

1 回答 1

1

我们在 gtsummary 中没有这样的功能。但是,我确实写了一个并将其添加到我编写的另一个名为 bstfun 的包中(仅限 GitHub)。这是一些 gtsummary 函数开始的地方,它们可能会在以后迁移到包中。

无论如何,这就是你如何获得你想要的桌子的方法。

devtools::install_github("ddsjoberg/bstfun")
library(bstfun)

tbl <-
  trial %>%
  tbl_2way_summary(grade, trt, marker)

在此处输入图像描述

于 2020-09-24T15:05:57.873 回答