Search code examples
rgtsummary

Show only the difference column when using add_difference function in gt_summary


When creating a table with the add_difference function like this

  trial %>%
  select(trt, age, marker, response, death) %>%
  tbl_summary(
    by = trt,
    missing = "no"
  ) %>%
  add_difference()

You get a table with the difference column and p_value and CI columns. enter image description here

I want to remove those columns as I want to add p_values for a nonparametric test for continuous variables, which to my understanding from this documentation, can't be done using add_difference.

If I use modify_column_hide(columns = c(p.value, ci)) and than add_p i'll get an error:

trial %>%
  select(trt, age, marker, response, death) %>% tbl_summary(
    by = trt,
    missing = "no" ) %>%
  add_difference() %>% 
  modify_column_hide(columns = c(p.value, ci)) %>% 
  add_p()

#Error: `add_p()` cannot be run after `add_difference()` or `add_p()` when a #'p.value' column is already present.

What is the correct approach to do so?


Solution

  • Here is how i would do it...

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.7.0'
    
    trial %>%
      select(trt, age, marker, response, death) %>% tbl_summary(
        by = trt,
        missing = "no" ) %>%
      add_difference() %>% 
      modify_column_hide(columns = c(p.value, ci)) %>% # hide the CI and p-value
      modify_table_body(~.x %>% select(-p.value)) %>% # remove the p-value from the table 
      modify_footnote(estimate = NA) %>% # remove difference footnote
      add_p() %>% # add new p-values
      as_kable() # convert to kable so it will display on stackoverflow
    
    Characteristic Drug A, N = 98 Drug B, N = 102 Difference p-value
    Age 46 (37, 59) 48 (39, 56) -0.44 0.7
    Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.20 0.085
    Tumor Response 28 (29%) 33 (34%) -4.2% 0.5
    Patient Died 52 (53%) 60 (59%) -5.8% 0.4

    Created on 2023-01-26 with reprex v2.0.2