I have multiple columns created from the "by" variable that I want mean() and sd(), but in one column (the last) I just want the mean(). Is there a way to format statistics by column as opposed to row with tbl_summary()?
df <- data.frame(
group = rep(c("A", "B", "C"), each = 10),
var1 = rnorm(30, mean = 5, sd = 2),
var2 = rnorm(30, mean = 7, sd = 1),
var3 = rnorm(30, mean = 9, sd = 3))
df |>
tbl_summary(
by = group,
statistic = all_continuous() ~ "{mean} ({sd})"
)
So with the above is there a way to display just {mean} for Group == "C" while keeping {mean} ({sd}) statistics for Group = c("A", "B")?
You could create separate tables and then merge them:
library(gtsummary)
df |>
subset(group != "C") |>
tbl_summary(
by = group,
statistic = all_continuous() ~ "{mean} ({sd})"
) -> gts1
df |>
subset(group == "C") |>
tbl_summary(
by = group,
statistic = all_continuous() ~ "{mean}"
) -> gts2
tbl_merge(list(gts1, gts2)) |>
modify_spanning_header(everything() ~ NA_character_)
Created on 2024-09-24 with reprex v2.0.2