When creating a summary table, split by group, the size of each group automatically shows up at the top of their respective columns. So the column headings look like this: Characteristic | 1, N = 100 | 2, N = 120. Code below:
library(dplyr)
library(gtsummary)
data %>%
select(group, age, sex) %>%
tbl_summary(by = group)
However, I would like to name my groups to something more meaningful than "1" and "2". For example, if my data consists of kids in a swim class, I would want to name the groups by the name of the swim class: ducks and turtles. So I do something like this:
library(dplyr)
library(gtsummary)
data %>%
select(group, age, sex) %>%
tbl_summary(by = group) %>%
modify_header(
update = list(
stat_1 ~ "**Ducks**",
stat_2 ~ "**Turtles**"))
modify_spanning_header(
update = starts_with("stat_") ~ "Swim Class Name")
This works! However, the size of each group disappears from the top of their respective columns. My work-around is to add in the size of each group manually, as part of the names. I have to leave a little note for myself to check the N for each group before adding it in. Like this:
library(dplyr)
library(gtsummary)
data %>%
select(group, age, sex) %>%
tbl_summary(by = group) %>%
modify_header(
update = list(
stat_1 ~ "**Ducks**, N = 100",
stat_2 ~ "**Turtles**, N = 120")) %>% # to check the N for each group, remove this to see default appearance which shows the N
modify_spanning_header(
update = starts_with("stat_") ~ "Swim Class Name")
This works but its error-prone as it requires me to double check the numbers then add them in manually.
How do I label the columns, representing each group, AND retain the numbers showing group sizes when splitting the summary table by group?
