ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.05k stars 124 forks source link

Feature request: Should `add_overall.tbl_summary()` accept a statistic argument? #1047

Closed larmarange closed 2 years ago

larmarange commented 2 years ago

I had a question from a colleague who wanted to display a cross table with just percentages in the main table, but with % and number of observations per row in total column.

It could be done by creating two tables and then using tbl_merge(). Here an example.

library(questionr)
library(gtsummary)
#> #BlackLivesMatter
library(labelled)
#> Warning: le package 'labelled' a été compilé avec la version R 4.1.2

data(hdv2003)
hdv2003 <- hdv2003 %>%
  set_variable_labels(
    nivetud = "Niveau d'étude",
    sexe = "Sexe"
  )

theme_gtsummary_language("fr", decimal.mark = ",")
#> Setting theme `language: fr`

tbl_by <- hdv2003 %>%
  tbl_summary(
    by = relig,
    include = c(nivetud, sexe),
    percent = "row",
    statistic = ~ "{p}%",
    digits = ~ 1
  ) %>%
  bold_labels() %>%
  modify_header(all_stat_cols(stat_0 = FALSE) ~ "**{level}**")

tbl_total <- hdv2003 %>%
  tbl_summary(
    include = c(nivetud, sexe),
    percent = "row",
    statistic = ~ "{p}% (n={n})",
    digits = ~ c(0, 0)
  ) %>%
  modify_header(stat_0 ~ "**Total**")

tbl_merge(
  list(tbl_by, tbl_total),
  tab_spanner = c("**Rapport à la religion**", NA)
) %>%
  modify_footnote(all_stat_cols() ~ NA) %>%
  as_kable()
Caractéristique Pratiquant regulier Pratiquant occasionnel Appartenance sans pratique Ni croyance ni appartenance Rejet NSP ou NVPR Total
Niveau d’étude
N’a jamais fait d’etudes 48,7% 20,5% 15,4% 7,7% 5,1% 2,6% 100% (n=39)
A arrete ses etudes, avant la derniere annee d’etudes primaires 23,3% 25,6% 38,4% 9,3% 2,3% 1,2% 100% (n=86)
Derniere annee d’etudes primaires 17,6% 26,7% 38,7% 11,7% 2,3% 2,9% 100% (n=341)
1er cycle 14,2% 25,5% 32,4% 20,1% 4,4% 3,4% 100% (n=204)
2eme cycle 13,7% 14,8% 39,3% 23,5% 6,0% 2,7% 100% (n=183)
Enseignement technique ou professionnel court 6,9% 24,6% 37,6% 23,5% 5,6% 1,7% 100% (n=463)
Enseignement technique ou professionnel long 8,4% 18,3% 43,5% 25,2% 3,1% 1,5% 100% (n=131)
Enseignement superieur y compris technique superieur 11,8% 19,7% 42,2% 19,7% 5,4% 1,1% 100% (n=441)
Manquant 18 17 34 35 7 1 112
Sexe
Homme 9,6% 18,9% 40,6% 23,4% 5,8% 1,8% 100% (n=899)
Femme 16,3% 24,7% 35,9% 17,2% 3,7% 2,2% 100% (n=1 101)

Created on 2021-12-06 by the reprex package (v2.0.1)

Somehow, it would be much easier if possible, in add_overall() to overwrite the statistic and the digits argument., i.e.

tbl %>%
  add_overall(
    statistic = ~ "{p}% (n={n})",
    digits = ~ c(0, 0)
  )
larmarange commented 2 years ago

Of course, default would be NULL and will consider values defined in the tbl_summary()