Home > OS >  tbl_merge: sort variables alphabetically in merged tbl_regression models {gtsummary}
tbl_merge: sort variables alphabetically in merged tbl_regression models {gtsummary}

Time:12-20

Consider the example below:

library(tidyverse)
library(gtsummary)

    t1 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age   ttdeath, .) %>%
      tbl_regression()
    t2 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age   response   death, .) %>%
      tbl_regression()
    t3 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age   stage   death, .) %>%
      tbl_regression()
    t4 <- trial %>%
      na.exclude() %>%
      lm(marker ~ stage   grade   death, .) %>%
      tbl_regression()
    
    tbl_merge(list(t1, t2, t3 ,t4))

enter image description here

In the characteristic tab, variables are only alphabetically within a particular model. For example, when looking only at Table 1 is fine, it's A-M-T.

However, when looking at mutiple tables at once, the order is not alphabetical. Is there a way to sort variables alphabetically within all the tables?

E.g. Age, Grade, Months to Death/Censor, Patient Died...

I assume the way to do this is by gtsummary::modify_table_body into dplyr::arrange, but I don't know how to do it exactly.

Any help would be appreciated. Thanks in advance.

Edit: actually, anyway to manually modify the order of variables would be helpful as well, ignore this if it's too difficult.

Edit2: I forgot to mention that there's glance statistics involved as well, such as add_glance_table(adj.r.squared) %>% modify_table_body(~.x %>% arrange(row_type == "glance_statistic")) (which is not in this example), so I imagine there is a possibility that the sorting of variables messes with the order of glance statistics as well. Excuse me for making this complicated, but it would be extremely helpful.

CodePudding user response:

You're correct that modify_table_body() is the way to go. You'll first want to arrange the order to put the glance table at the bottom, then you arrange by "var_label" to put the variables in alphabetical order. "var_label" is a column in .$table_body. Example below!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'

t1 <- trial %>%
  lm(marker ~ age   ttdeath, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t2 <- trial %>%
  lm(marker ~ age   response   death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t3 <- trial %>%
  lm(marker ~ age   stage   death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t4 <- trial %>%
  lm(marker ~ stage   grade   death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)

tbl <- 
  tbl_merge(list(t1, t2, t3 ,t4)) %>%
  modify_table_body(
    ~.x %>% 
      dplyr::arrange(
        row_type == "glance_statistic", # sort glance table to bottom
        var_label                       # sort by the variable label (a hidden column) 
      )
  )

enter image description here Created on 2021-12-19 by the enter image description here

  • Related