rgtsummary

Modifying digits in multi-line continuous summary table? {gtsummary}


I'm trying to create a multi-line summary table for a continuous variable using gtsummary, with the appropriate digits displayed per row. I have outlined my the relevant arguments in the statistics and digits functions, however the corresponding number of digits are not displaying correctly in the output table. (e.g., kurtosis is reading as 0 when it should say 0.34)

The challenge is that all of the statistics are attached to the same variable name (in this case, qsec), so I am not able to specify digit numbers per statistic to any certain variable. Any guidance would be greatly appreciated!

library(gtsummary)
library(psych)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union


skew(mtcars$qsec)
#> [1] 0.3690453
kurtosi(mtcars$qsec)
#> [1] 0.3351142

mtcars %>%
  select(qsec) %>%
  tbl_summary(
    type = qsec ~ "continuous2",
    statistic = all_continuous() ~ c("{N_nonmiss}",
                                     "{mean} ({sd})", 
                                     "{median} ({p25}, {p75})", 
                                     "{min}, {max}",
                                     "{skew}",
                                     "{kurtosi}"),
    
    digits = all_continuous() ~  c(0,
                                   1,
                                   1,
                                   0,
                                   2,
                                   2),
   label = list(variable = ""),
    missing = "always",
    missing_text = "NA"
  ) %>% 
  add_stat_label(label = list(all_continuous() ~ c("N",
                                                   "Mean (SD)",
                                                   "Median (Q1, Q3)",
                                                   "Min, max",
                                                   "Skewness",
                                                   "Kurtosis")))

Table output can be found here:


Solution

  • in your statistic = declataration you have 10 statistics, therefore you would need as much as 10 digits declarations so as to control them all

    #fragment
       digits = all_continuous() ~  c(0,
                                       1,1,
                                       1,1,1,
                                       0,0,
                                       2,
                                       2)