Home > Net >  Add labels to group similar variables in a gtsummary generated table
Add labels to group similar variables in a gtsummary generated table

Time:03-12

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:

Image adapted from tbl_summary output

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 enter image description here

Created on 2022-03-11 by the reprex package (v2.0.1)

  • Related