I am trying to create table like below using gtsummary.
I have sample code below that provides most of it.
# Load data
advs <- pharmaverseadam::advs %>%
filter(SAFFL == "Y" & VSTESTCD %in% c('SYSBP', "DIABP") & !is.na(AVISIT)) %>%
select(c(USUBJID, TRT01A, PARAMCD, PARAM, AVISIT, AVISITN, ADT, AVAL, CHG, PCHG, VSPOS, VSTPT))
# Summary mean prior to process
advs.smr <- advs %>%
group_by(USUBJID, TRT01A, PARAMCD, PARAM, AVISIT, AVISITN, ADT) %>%
summarise(AVAL.MEAN = mean(AVAL, na.rm = TRUE),
CHG.MEAN = mean(CHG, na.rm = TRUE),
.groups = 'drop') %>%
mutate(visit_id = paste("Vis", sprintf("%03d", AVISITN), AVISIT, sep = "_")) %>%
arrange(USUBJID, PARAMCD, AVISITN) %>%
filter(AVISITN <= 4)
# Wide to Long
advs.smr.l <- advs.smr %>%
pivot_longer(cols = c(AVAL.MEAN, CHG.MEAN),
names_to = "anls_var",
values_to = "Value") %>%
filter(!is.nan(Value)) %>%
mutate(anls_var = if_else(grepl("AVAL", anls_var), "Actual Value", "Change From Baseline"))
# Long to Wide
vs.parm <- advs.smr.l %>%
select(-c(AVISITN, AVISIT, ADT)) %>%
pivot_wider(names_from = visit_id,
values_from = Value) %>%
filter(PARAMCD == "SYSBP")
# Upcase column names
colnames(vs.parm) <- toupper(colnames(vs.parm))
# Create List of visit names
alvis <- unique(colnames(vs.parm)[grep("^VIS", colnames(vs.parm), ignore.case = TRUE)])
vis.nam <- setNames(as.list(sub(".*_", "", alvis)), alvis)
# Create table body
vs.parm %>%
tbl_strata(
strata = TRT01A,
.tbl_fun = ~.x %>%
tbl_summary(
by = ANLS_VAR,
include = c(starts_with("VIS")),
type = c(starts_with("VIS")) ~ "continuous2",
statistic = c(starts_with("VIS")) ~ c("{N_nonmiss}", "{mean} ({sd})", "{median}", "{min}, {max}"),
digits = list(all_continuous() ~ c(1, 2, 3, 2, 1, 1)),
label = vis.nam,
missing = "no") %>%
# Update Stat Labels
add_stat_label(
label = list(all_continuous() ~ c("n", "MEAN (SD)", "MEDIAN", "MIN, MAX"))) %>%
# Update header
modify_header(
label ~ "Visit",
all_stat_cols() ~ "**{level}**") %>%
# Remove default footnote
remove_footnote_header(columns = all_stat_cols()),
.header = "**{strata}** <br>(N = {n})"
)
Below is a screenshot of the output. There are couple of issues I am having.
- The N counts for header are driven by both AVAL & CHG records. These are doubled. (yellow highlights below)
- Any way to suppress the warnings on Baseline related to CHG. I understand these are genuine & it's ok if we can't.
- What is the best way to make the purple boxed part in screenshot blank? Thinking to use modify_table_body - not sure if there's a better way.


