簡體   English   中英

gtsummary:通過兩個分類變量總結一個連續變量

[英]gtsummary: summarizing a continuous variables by two categorical variables

我試圖通過兩個分類變量來總結一個連續變量,如下所示。 我無法正確執行此操作。 我想知道是否有辦法通過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

我們沒有專門在 gtsummary 中提供這樣的功能。 但是,我確實編寫了一個並將其添加到我編寫的另一個名為 bstfun 的包中(僅限 GitHub)。 這是一些 gtsummary 函數開始的地方,它們可能會在以后遷移到包中。

無論如何,這是您獲得所需桌子的方法。

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

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM