Reputation: 163
How do you add labels to group similar variables in a gtsummary generated table? For example, if survey respondents are able to select multiple races (captured in separate variables), I would like a heading for the race variables:
Here is the tbl_summary output:
library(tidyverse)
library(gtsummary)
library(knitr)
tibble::tribble(
~race___1, ~race___2, ~race___3, ~race___4, ~race___5,
"No", "No", "No", "No", "Yes",
"No", "No", "Yes", "No", "No",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes"
) %>% tbl_summary() %>% as_kable()
Characteristic | N = 10 |
---|---|
race___1 | |
No | 10 (100%) |
race___2 | |
No | 10 (100%) |
race___3 | 1 (10%) |
race___4 | |
No | 10 (100%) |
race___5 | 9 (90%) |
Created on 2022-03-11 by the reprex package (v2.0.1)
Upvotes: 1
Views: 617
Reputation: 11595
You'll want to use the bstfun::add_variable_grouping()
function (https://www.danieldsjoberg.com/bstfun/reference/add_variable_grouping.html).
Example below!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'
tbl <-
tibble::tribble(
~race___1, ~race___2, ~race___3, ~race___4, ~race___5,
"No", "No", "No", "No", "Yes",
"No", "No", "Yes", "No", "No",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes",
"No", "No", "No", "No", "Yes"
) %>%
tbl_summary(value = everything() ~ "Yes") %>%
bstfun::add_variable_grouping(
"Race" = c("race___1", "race___2", "race___3", "race___4", "race___5")
)
Created on 2022-03-11 by the reprex package (v2.0.1)
Upvotes: 2