gtsummary R package: how to remove commas when printing large numbers?

Viewed 468

When creating a table using tbl_summary() from the gtsummary R package, the default settings will print commas for large numbers. Is it possible to remove these commas?

For example, suppose I want to calculate the median and IQR for the following variable which has a large scale:

set.seed(123)
data=data.frame(X=rnorm(100,mean=50000,sd=10000))
tbl_summary(data)

enter image description here

I would like to print the large numbers without using commas, although still use a comma to separate the 1st and 3rd quantile statistics within the parentheses. So the output should look like: 50618 (45061, 56918). Is this possible?

Thanks

1 Answers

Yep, you can control that!

The function that prints all numbers in a gtsummary table is style_number() (essentially, a copy of scales::number() but customizable with gtsummary options). We can set the default style_number(big.mark=) argument using gtsummary themes (more info here http://www.danieldsjoberg.com/gtsummary/articles/themes.html).

This code below will set the big mark separator to "" instead of the default comma.

list("style_number-arg:big.mark" = "") %>%
  set_gtsummary_theme()

ggplot2::diamonds %>%
  select(cut) %>%
  tbl_summary()

But, that code is a little tricky to remember. The most common place where the big mark and decimal mark are changed is when the tables are printed into different languages (e.g. where they may use comma as the decimal separator and a period as the big mark separator). Because of this, this is built into the language theme. The code below sets the language to English (already the default language), and removes the comma as the big mark separator.

theme_gtsummary_language("en", big.mark = "")

ggplot2::diamonds %>%
  select(cut) %>%
  tbl_summary()

enter image description here

Happy Coding!

Related