Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtsummary table: replace empty cell information with -

Tags:

r

gtsummary

I'm using the package gtsummary to create a table and I would like to replace the value in empty cells with "-" instead of "0 (0%)".

Is this possible?

an example of the table I am getting can be seen here:

library(gtsummary)

data <- data.frame(letter = c(rep("a", times = 3), rep("b", times = 3)),
                   value = c(rep(1, times = 3), rep(2, times = 3)))

data %>% tbl_summary(by = value)

Kind regards Mathias

like image 996
mahol Avatar asked Mar 14 '26 23:03

mahol


1 Answers

We can manually manipulate the result, although I think this may be less ideal. I renamed data to df to avoid conflicts with the data function.

 library(dplyr) #probably not necessary since gtsummary uses tibble anyway
    df %>% tbl_summary(by = value) -> res
    
    res[1]$table_body <-res[1]$table_body %>% 
      mutate(across(c(stat_1, stat_2), ~gsub("^0.*", "-",.)))
    res

enter image description here

like image 186
NelsonGon Avatar answered Mar 16 '26 12:03

NelsonGon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!