mala fama
mala fama

Reputation: 33

How can I use 2 times the same argument in tbl_summary()

I want to use 2 times the digits= argument in tbl_summary but I can't. Once it would be like this to specify the number of decimal digits

digits = list(all_categorical() ~ c(0,1))

and the second one to indicate that there is no space as a separator for big numbers

digits = ~label_style_number(big.mark = "")

But it doesn't work in the same code

df %>% %>%
  mutate_all(as.factor) %>%
  tbl_summary(statistic = list(all_categorical() ~ "{n} ({p})"),
              digits = list(all_categorical() ~ c(0,1)),
              digits = list(a ~ label_style_number(big.mark = "")))

Error in tbl_summary(., statistic = list(all_categorical() ~ “{n} ({p}”), : 
  formal argument “digits” matches multiple specified arguments.

also tried this

df %>%
  mutate_all(as.factor) %>%
  tbl_summary(
    by= a,
    missing = "always",
    statistic = list(all_categorical() ~ "{n} ({p})"),
    sort = all_categorical(FALSE) ~ "frequency",
    digits = list(all_categorical() ~ c(0, 1),
                  b ~ label_style_number(big.mark = "")))

but it returns a 0 in the decimals for all values

How can I have both things?

This is the minimal reproducible example I use:

df<-
  tibble(a= sample(c('red', 'blue', 'pink'),
                 size= 10000,
                 replace= T),
         b= sample(c('car', 'bike', 'boat'),
                   size= 10000,
                   replace= T))

Upvotes: 3

Views: 59

Answers (2)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11764

There are two ways to do this. The first looks most like the code in your example, and the second I find a bit easier to use.

  1. The digits argument can accept integers, but these are aliases that are converted into functions that round and convert the result to a character. You cannot combine big.mark specification with this shortcut. In this case, you'll need to pass functions (functions are returned from the label_*() functions) with the big.mark specified.
library(gtsummary)

rep(list(trial), 15) |>
  dplyr::bind_rows() |> 
  tbl_summary(
    include = grade,
    digits = all_categorical() ~ list(p = label_style_percent(big.mark = ""),
                                      n = label_style_number(big.mark = ""))
  ) |> 
  modify_header(all_stat_cols() ~ "**N = {style_number(n, big.mark = '')}**") |> 
  as_kable() # convert to markdown so table renders on SO
Characteristic N = 3000
grade
I 1020 (34%)
II 1020 (34%)
III 960 (32%)

Created on 2025-02-10 with reprex v2.1.1

The {gtsummary} package supports translations into many languages (~12 I believe). When we set the translation language, we can also set the big mark and the decimal mark to match that country's convention. Once set, all numbers printed with gtsummary will follow that convention.

In the example below, I set the language to English (the default, so no change there), and big.mark="".

library(gtsummary)

theme_gtsummary_language(language = "en", big.mark = "")
#> Setting theme "language: en"
rep(list(trial), 15) |>
  dplyr::bind_rows() |> 
  tbl_summary(include = grade) |> 
  as_kable() # convert to markdown so table renders on SO
Characteristic N = 3000
grade
I 1020 (34%)
II 1020 (34%)
III 960 (32%)

Created on 2025-02-10 with reprex v2.1.1

Hope that helps!

Upvotes: 0

Ahmed Amin Shahin
Ahmed Amin Shahin

Reputation: 1143

library(gtsummary)
library(dplyr)

# Example data
df <- tibble(
  a = sample(c('red', 'blue', 'pink'), size = 10000, replace = TRUE),
  b = sample(c('car', 'bike', 'boat'), size = 10000, replace = TRUE)
)

# Apply tbl_summary with custom formatting
df %>%
  mutate_all(as.factor) %>%
  tbl_summary(
    by = a,
    missing = "always",
    statistic = list(all_categorical() ~ "{n} ({p})"),
    sort = all_categorical(FALSE) ~ "frequency",
    digits = list(all_categorical() ~ c(0, 1)) # Round percentages to 1 decimal place
  ) %>%
  # Use modify_fmt_fun to apply big.mark formatting to summary statistics columns
  modify_fmt_fun(
    update = starts_with("stat_") ~ style_number(big.mark = "")
  )

Upvotes: -1

Related Questions