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
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.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 thestatistic
and thedigits
argument., i.e.