I want to create a table stratified three ways: I'd like the columns to represent one variable (location), and the rows to display other variables (age group and sex), stratified by a second variable (gcs). I also want to include the missing values in the table.
Example dataframe here:
dat <- data.frame(id = 1:20,
age_group = as.factor(c("66-70", "71-75", "61-65", "76-80", "61-65", "81-85", "76-80","66-70","81-85","66-70",
"66-70", "81-85","81-85","76-80","61-65","76-80","66-70","81-85","66-70","71-75" )),
location = as.factor(c("indoor", NA, "indoor", "indoor", "outdoor","indoor", "outdoor", "indoor", "indoor", "outdoor",
"indoor", "outdoor","indoor", NA,"outdoor", "outdoor","indoor", "outdoor","indoor", "outdoor")),
sex = as.factor(c("M", "F", "M", "F", NA, "M", "F", "M", "F", "M","F", "M", "F", "M", "F", "F", "M", "F", "F", "M")),
gcs = as.factor(c("moderate", "severe", "minor", "minor", "minor", "severe", "minor", "moderate", NA, "minor",
"moderate", "minor", NA, "minor", "severe", "moderate", "minor", "severe", "minor", "severe")))
The closest I've gotten is here:
table <- tbl_strata(data = dat,
strata = location,
.tbl_fun =
~.x %>%
tbl_summary( include = ! id,
by = gcs, missing = "always"))
But I'd like the rows to be stratified by gcs rather than the columns like this
Thanks!
Is this what you're after?
library(gtsummary)
dat <- data.frame(id = 1:20,
age_group = as.factor(c("66-70", "71-75", "61-65", "76-80", "61-65", "81-85", "76-80","66-70","81-85","66-70",
"66-70", "81-85","81-85","76-80","61-65","76-80","66-70","81-85","66-70","71-75" )),
location = as.factor(c("indoor", NA, "indoor", "indoor", "outdoor","indoor", "outdoor", "indoor", "indoor", "outdoor",
"indoor", "outdoor","indoor", NA,"outdoor", "outdoor","indoor", "outdoor","indoor", "outdoor")),
sex = as.factor(c("M", "F", "M", "F", NA, "M", "F", "M", "F", "M","F", "M", "F", "M", "F", "F", "M", "F", "F", "M")),
gcs = as.factor(c("moderate", "severe", "minor", "minor", "minor", "severe", "minor", "moderate", NA, "minor",
"moderate", "minor", NA, "minor", "severe", "moderate", "minor", "severe", "minor", "severe")))
table <-
tbl_strata(
data = dat,
strata = age_group,
.tbl_fun =
~.x %>%
tbl_summary(
include = gcs,
by = location,
missing = "always"
),
.combine_with = "tbl_stack"
)
#> 1 observations missing `location` have been removed. To include these observations, use `forcats::fct_na_value_to_level()` on `location` column before passing to `tbl_summary()`.
#> 1 observations missing `location` have been removed. To include these observations, use `forcats::fct_na_value_to_level()` on `location` column before passing to `tbl_summary()`.
#> ℹ Column headers among stacked tables differ. Headers from the first table are
#> used. Use `quiet = TRUE` to supress this message.
Created on 2023-08-24 with reprex v2.0.2