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