Susan M
Susan M

Reputation: 11

How do I format digits in missing_stat in gtsummary::tbl_summary

I would like to format the digits in missing_stat, in particular {p_miss} to have 2 significant digits when used in missing_stat.

For example, if p_miss is 0.01666 the default display is 1.7%. I need the display to be 1.67%%

My R code looks like:

data <- data.frame(
  group = c(rep("A", 50), rep("B", 9), rep(NA, 1)),
  height = c(rnorm(30, mean = 160, sd = 5), rnorm(30, mean = 170, sd = 6)),
  gender = c(rep("Male", 25), rep("Female", 34), rep(NA, 1))  
)

tableT <- data %>%
  gtsummary::tbl_summary(
    type =         list(all_continuous() ~ "continuous2",
                        all_dichotomous() ~ "categorical"),
    
    statistic =     list(all_continuous2() ~ "{mean} ({sd})",
                         all_categorical() ~"{n} ({p}%)"),
    
    missing =      "always",
    
    missing_text = "Missing",
    
    missing_stat = "{N_miss}/{N_obs} ({p_miss})%)",
    
    digits =        list(all_categorical() ~ c(0, 2),
                         all_continuous2() ~ c(2,2)),
    
    include = everything()
  )

I want the same percentage formatting as in the continuous variable count and percent values:

enter image description here

I've tried changing the digits argument -

digits = list(all_categorical() ~ c(0, 2),
              all_continuous2() ~ c(2,2)
              everything() ~ (p_miss = label_style_number(digits=2))

I've tried adding

modify_fmt_fun(statistic \~ function(x) style_number(.x, digits = 2))

Upvotes: 1

Views: 61

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11764

It makes sense you are stumped, because this is not documented anywhere :| I am still thinking on the best way to expose this functionality to the users, but it hasn't been a top priority yet.

Hopefully, there will be a smoother solution in the near future. But this will work for now.

library(gtsummary)

# change default percent styling function for `tbl_summary()`
list("tbl_summary-fn:percent_fun" = label_style_number(scale = 100, digits = 2)) |> 
  set_gtsummary_theme()

data.frame(group = c(rep("A", 50), rep("B", 9), rep(NA, 1))) |> 
  tbl_summary(
    missing =      "always",
    missing_text = "Missing",
    missing_stat = "{N_miss}/{N_obs} ({p_miss})%)"
  ) |> 
  as_kable() # convert to kable to display on SO
Characteristic N = 60
group
A 50 (84.75%)
B 9 (15.25%)
Missing 1/60 (1.67)%)

Created on 2025-01-18 with reprex v2.1.1

Upvotes: 0

Related Questions