rmediangtsummary

gtsummary::tbl_summary gives different flavours of tables


I am using gtsummary::tbl_summary on R4.4. I received a table for it using the following df:

structure(list(Treatment = c("Verum", "Verum", "SoC", "SoC", "Verum"), PTN = c(7.91749000549316, 7.60503005981445, 8.45341014862061, 8.48719024658203, 7.39421987533569), HGF = c(13.0622499583508, 12.6070907710339, 12.3543298838879, 12.2434056399609, 13.6388056872632 ), FGF_21 = c(5.53692184768091, 5.28081165633569, 5.68246208510767, 5.6471217759646, 4.85877167067895), PVALB = c(8.4968900680542, 4.57206010818481, 7.59231996536255, 8.43292999267578, 6.51680994033813 )), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame" ))

The wrong table I received has the following aspect.

I would like to receive for each different protein the median of each treatment, like this desired

So far I have been using the following :

df %>% tbl_summary(by = "Treatment") %>% add_p() %>%  sort_p() %>% modify_header(label ~ "**Treatment**") %>%  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received at day = 8**") %>% bold_labels()

Thanks for any help Regards Andrea


Solution

  • You have to force tbl_summary into treating your variables as continuous, otherwise, it will treat them as categorical, since there are too few distinct values (need at least 10 I think, according to the assign_summary_type help page).

    df %>% tbl_summary(by = Treatment,
                       type=list(everything()~"continuous")) %>% 
      add_p() %>%  
      sort_p() %>% 
      modify_header(label ~ "**Treatment**") %>%  
      modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received at day = 8**") %>% 
      bold_labels()
    

    enter image description here