StHe
StHe

Reputation: 37

change the summary statistics in gtsummary table

I've created a summary table with gtsummary. But I want to make some changes to the summary statistics. I would like the % to be a percentage of the total sum for each level, and not the entire variable. For grade 1 the total sum would be 68. So for drug A I want it to say 35(51.47%) (35/68x100). Similarly for drug B it would show 33(48.53%). How would I do that? Thanks a lot!

library(gtsummary)
data(trial)
trial2 <- trial %>% select(trt, grade)
trial2 %>%
  tbl_summary(
    by = trt,
    statistic = list(all_categorical() ~ "{n} ({p}%)"))

enter image description here

Upvotes: 0

Views: 1213

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174468

You can add percent = "row" to your tbl_summary call

trial2 %>%
  tbl_summary(
    by = trt,
    statistic = list(all_categorical() ~ "{n} ({p}%)"),
    percent = 'row')

enter image description here

Upvotes: 4

Related Questions