Reputation: 45
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
Upvotes: 3
Views: 1071
Reputation: 435
Here is a method using the built-in gtsummary
function:
library(gtsummary)
data %>%
gtsummary::tbl_summary(by = value) %>%
gtsummary::modify_table_body(
~.x %>%
mutate(
across(all_stat_cols(), ~gsub("^0.*", "-", .))
)
)
Upvotes: 1
Reputation: 13319
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
Upvotes: 5