It performs a univariate statistical analysis of a dataset
by group and formats the results so that they can be used with
the tabulator()
function or directly with as_flextable.
Usage
summarizor(x, by = character(), overall_label = NULL)
Arguments
- x
dataset
- by
columns names to be used as grouping columns
- overall_label
label to use as overall label
Examples
z <- summarizor(CO2[-c(1, 4)],
by = "Treatment",
overall_label = "Overall"
)
ft_1 <- as_flextable(z)
ft_1
nonchilled
(N=40)
chilled
(N=42)
Overall
(N=82)
Plant
Qn1
5 (12.50%)
0 (0.00%)
5 (6.10%)
Qn2
7 (17.50%)
0 (0.00%)
7 (8.54%)
Qn3
7 (17.50%)
0 (0.00%)
7 (8.54%)
Qc1
0 (0.00%)
7 (16.67%)
7 (8.54%)
Qc3
0 (0.00%)
7 (16.67%)
7 (8.54%)
Qc2
0 (0.00%)
7 (16.67%)
7 (8.54%)
Mn3
7 (17.50%)
0 (0.00%)
7 (8.54%)
Mn2
7 (17.50%)
0 (0.00%)
7 (8.54%)
Mn1
7 (17.50%)
0 (0.00%)
7 (8.54%)
Mc2
0 (0.00%)
7 (16.67%)
7 (8.54%)
Mc3
0 (0.00%)
7 (16.67%)
7 (8.54%)
Mc1
0 (0.00%)
7 (16.67%)
7 (8.54%)
Missing
0 (0.00%)
0 (0.00%)
0 (0.00%)
Type
Quebec
19 (47.50%)
21 (50.00%)
40 (48.78%)
Mississippi
21 (52.50%)
21 (50.00%)
42 (51.22%)
Missing
0 (0.00%)
0 (0.00%)
0 (0.00%)
conc
Mean (SD)
445.6 (299.9)
435.0 (297.7)
440.2 (297.0)
Median (IQR)
350.0 (500.0)
350.0 (500.0)
350.0 (500.0)
Range
95.0 - 1000.0
95.0 - 1000.0
95.0 - 1000.0
Missing
0 (0.00%)
0 (0.00%)
0 (0.00%)
uptake
Mean (SD)
30.8 (9.6)
23.8 (10.9)
27.2 (10.8)
Median (IQR)
31.3 (12.3)
19.7 (20.4)
28.3 (18.8)
Range
10.6 - 45.5
7.7 - 42.4
7.7 - 45.5
Missing
0 (0.00%)
0 (0.00%)
0 (0.00%)
# version 2 with your own functions ----
n_format <- function(n, percent) {
z <- character(length = length(n))
wcts <- !is.na(n)
z[wcts] <- sprintf("%.0f (%.01f %%)",
n[wcts], percent[wcts] * 100)
z
}
stat_format <- function(stat, num1, num2,
num1_mask = "%.01f",
num2_mask = "(%.01f)") {
z_num <- character(length = length(num1))
is_mean_sd <- !is.na(num1) & !is.na(num2) & stat %in% "mean_sd"
is_median_iqr <- !is.na(num1) & !is.na(num2) &
stat %in% "median_iqr"
is_range <- !is.na(num1) & !is.na(num2) & stat %in% "range"
is_num_1 <- !is.na(num1) & is.na(num2)
z_num[is_num_1] <- sprintf(num1_mask, num1[is_num_1])
z_num[is_mean_sd] <- paste0(
sprintf(num1_mask, num1[is_mean_sd]),
" ",
sprintf(num2_mask, num2[is_mean_sd])
)
z_num[is_median_iqr] <- paste0(
sprintf(num1_mask, num1[is_median_iqr]),
" ",
sprintf(num2_mask, num2[is_median_iqr])
)
z_num[is_range] <- paste0(
"[",
sprintf(num1_mask, num1[is_range]),
" - ",
sprintf(num1_mask, num2[is_range]),
"]"
)
z_num
}
tab_2 <- tabulator(z,
rows = c("variable", "stat"),
columns = "Treatment",
`Est.` = as_paragraph(
as_chunk(stat_format(stat, value1, value2))),
`N` = as_paragraph(as_chunk(n_format(cts, percent)))
)
ft_2 <- as_flextable(tab_2, separate_with = "variable")
ft_2
nonchilled
(N=40)
chilled
(N=42)
Overall
(N=82)
Est.
N
Est.
N
Est.
N
Plant
Qn1
5 (12.5 %)
0 (0.0 %)
5 (6.1 %)
Qn2
7 (17.5 %)
0 (0.0 %)
7 (8.5 %)
Qn3
7 (17.5 %)
0 (0.0 %)
7 (8.5 %)
Qc1
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Qc3
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Qc2
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Mn3
7 (17.5 %)
0 (0.0 %)
7 (8.5 %)
Mn2
7 (17.5 %)
0 (0.0 %)
7 (8.5 %)
Mn1
7 (17.5 %)
0 (0.0 %)
7 (8.5 %)
Mc2
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Mc3
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Mc1
0 (0.0 %)
7 (16.7 %)
7 (8.5 %)
Missing
0 (0.0 %)
0 (0.0 %)
0 (0.0 %)
Type
Quebec
19 (47.5 %)
21 (50.0 %)
40 (48.8 %)
Mississippi
21 (52.5 %)
21 (50.0 %)
42 (51.2 %)
Missing
0 (0.0 %)
0 (0.0 %)
0 (0.0 %)
conc
Mean (SD)
445.6 (299.9)
435.0 (297.7)
440.2 (297.0)
Median (IQR)
350.0 (500.0)
350.0 (500.0)
350.0 (500.0)
Range
[95.0 - 1000.0]
[95.0 - 1000.0]
[95.0 - 1000.0]
Missing
0 (0.0 %)
0 (0.0 %)
0 (0.0 %)
uptake
Mean (SD)
30.8 (9.6)
23.8 (10.9)
27.2 (10.8)
Median (IQR)
31.3 (12.3)
19.7 (20.4)
28.3 (18.8)
Range
[10.6 - 45.5]
[7.7 - 42.4]
[7.7 - 45.5]
Missing
0 (0.0 %)
0 (0.0 %)
0 (0.0 %)