Search code examples
rgtsummary

gtsummary table: replace empty cell information with -


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


Solution

  • 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