I am trying to summarize a continuous variable by two categorical variables as seen below. I not able to do this correctly. I wonder if there is a way to get this with the gtsummary
package. Thank you
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