Search code examples
rgtsummary

How to write a gtsummary with CI and overall column in last column


I would like to write a summary with confidence intervals for categorical variables and an overall column at the end of the table.

However using the following code, the overall column is placed at the front:

library(gtsummary)
library(tidyverse)

trial %>%
  tbl_summary(
    by = trt,
    include = "stage") %>%
  add_overall(last = TRUE)%>% 
  add_ci()

Why is the overall column at the beginning, even though I set last = TRUE? Overall column in the front


Solution

  • See below for a solution to re-order columns in a gtsummary table.

    library(gtsummary)
    
    trial %>%
      tbl_summary(
        by = trt,
        include = "stage") %>%
      add_overall(last = TRUE) %>%
      add_ci() %>%
      modify_table_body(
        ~dplyr::relocate(.x, stat_0, ci_stat_0, .after = last_col() )
      ) %>%
      as_kable()
    
    Characteristic Drug A, N = 98 95% CI Drug B, N = 102 95% CI Overall, N = 200 95% CI
    T Stage
    T1 28 (29%) 20%, 39% 25 (25%) 17%, 34% 53 (27%) 21%, 33%
    T2 25 (26%) 17%, 35% 29 (28%) 20%, 38% 54 (27%) 21%, 34%
    T3 22 (22%) 15%, 32% 21 (21%) 13%, 30% 43 (22%) 16%, 28%
    T4 23 (23%) 16%, 33% 27 (26%) 18%, 36% 50 (25%) 19%, 32%

    Created on 2023-06-15 with reprex v2.0.2

    I filed a bug report with gtsummary here https://github.com/ddsjoberg/gtsummary/issues/1525