3

Using the example on gtsummarytbl_summary page (http://www.danieldsjoberg.com/gtsummary/articles/tbl_summary.html):

  trial %>%
  select(trt, marker, stage)

tbl_summary(trial2)

Is it possible to change the names of the rows under trt? For example the table above gives trt as DrugA and DrugB. But could I have it labelled in the summary table as "cisplatin" for DrugA and "carboplatin" for DrugB, without changing the data frame?

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
pkreville
  • 33
  • 4

1 Answers1

3

The tbl_summary() function reports the data in the data frame. So if you'd like to report cisplatin and carboplatin, you change it in the data frame before you pass the data frame to tbl_summary(). Here's an example:

library(gtsummary)
library(tidyverse)

trial %>%
  select(trt, age) %>%
  mutate(
    trt = case_when(trt == "Drug A" ~ "Cisplatin",
                    trt == "Drug B" ~ "Carboplatin")
  ) %>%
  tbl_summary(label = trt ~ "Chemotherapy")

enter image description here

Happy Coding!

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • 1
    This is exactly what I was looking for. Thank you! – pkreville Jun 29 '20 at 22:07
  • Hi Thanks for this package, and this trick! I am hoping to use something similar in an automation pipeline Can I ask if you think this trick is stable? Or, if it isn't, are you planning on implementing something that allows for raw md/html formatting of any/all cells? Would really appreciate a comment from you, thanks again! – niklz Dec 02 '20 at 12:23
  • 1
    This is stable. We'll always report the data that is in the data frame passed to `tbl_summary()` – Daniel D. Sjoberg Dec 02 '20 at 13:46
  • Oh whoops! This was meant for another reply you made.. to do with sub/superscripting. Appologies – niklz Dec 02 '20 at 15:14