This function was written to allow easy demonstrations of flextable's ability to produce table summaries (with summarizor()). It assumes that we have either a quantitative variable, in which case we will display the mean and the standard deviation, or a qualitative variable, in which case we will display the count and the percentage corresponding to each modality.

fmt_2stats(
  stat,
  num1,
  num2,
  cts,
  pcts,
  num1_mask = "%.01f",
  num2_mask = "(%.01f)",
  cts_mask = "%.0f",
  pcts_mask = "(%.02f%%)"
)

fmt_summarizor(
  stat,
  num1,
  num2,
  cts,
  pcts,
  num1_mask = "%.01f",
  num2_mask = "(%.01f)",
  cts_mask = "%.0f",
  pcts_mask = "(%.02f%%)"
)

Arguments

stat

a character column containing the name of statictics

num1

a numeric statistic to display such as a mean or a median

num2

a numeric statistic to display such as a standard deviation or a median absolute deviation.

cts

a count to display

pcts

a percentage to display

num1_mask

format associated with num1, a format string used by sprintf().

num2_mask

format associated with num2, a format string used by sprintf().

cts_mask

format associated with cts, a format string used by sprintf().

pcts_mask

format associated with pcts, a format string used by sprintf().

See also

summarizor(), tabulator(), mk_par()

Other text formatter functions: fmt_avg_dev(), fmt_header_n(), fmt_n_percent()

Examples

library(flextable)
z <- summarizor(iris, by = "Species")

tab_1 <- tabulator(
  x = z,
  rows = c("variable", "stat"),
  columns = "Species",
  blah = as_paragraph(
    as_chunk(
      fmt_summarizor(
        stat = stat,
        num1 = value1, num2 = value2,
        cts = cts, pcts = percent
      )
    )
  )
)

ft_1 <- as_flextable(x = tab_1, separate_with = "variable")
ft_1 <- labelizor(
  x = ft_1, j = "stat",
  labels = c(mean_sd = "Moyenne (ecart-type)",
             median_iqr = "Mediane (IQR)",
             range = "Etendue",
             missing = "Valeurs manquantes"
             )
)
ft_1 <- autofit(ft_1)
ft_1
#> a flextable object.
#> col_keys: `variable`, `stat`, `dummy1`, `setosa@blah`, `dummy2`, `versicolor@blah`, `dummy3`, `virginica@blah` 
#> header has 1 row(s) 
#> body has 16 row(s) 
#> original dataset sample: 
#>       variable       stat value1@setosa value1@versicolor value1@virginica
#> 1 Sepal.Length    mean_sd         5.006             5.936            6.588
#> 2 Sepal.Length median_iqr         5.000             5.900            6.500
#> 3 Sepal.Length      range         4.300             4.900            4.900
#> 4 Sepal.Length    missing            NA                NA               NA
#> 5  Sepal.Width    mean_sd         3.428             2.770            2.974
#>   value2@setosa value2@versicolor value2@virginica cts@setosa cts@versicolor
#> 1     0.3524897         0.5161711        0.6358796         NA             NA
#> 2     0.4000000         0.7000000        0.6750000         NA             NA
#> 3     5.8000000         7.0000000        7.9000000         NA             NA
#> 4            NA                NA               NA          0              0
#> 5     0.3790644         0.3137983        0.3224966         NA             NA
#>   cts@virginica percent@setosa percent@versicolor percent@virginica
#> 1            NA             NA                 NA                NA
#> 2            NA             NA                 NA                NA
#> 3            NA             NA                 NA                NA
#> 4             0              0                  0                 0
#> 5            NA             NA                 NA                NA
#>   data_type@setosa data_type@versicolor data_type@virginica blah@setosa
#> 1       continuous           continuous          continuous            
#> 2       continuous           continuous          continuous            
#> 3       continuous           continuous          continuous            
#> 4       continuous           continuous          continuous            
#> 5       continuous           continuous          continuous            
#>   blah@versicolor blah@virginica dummy1 setosa@blah dummy2 versicolor@blah
#> 1                                                                         
#> 2                                                                         
#> 3                                                                         
#> 4                                                                         
#> 5                                                                         
#>   dummy3 virginica@blah
#> 1                      
#> 2                      
#> 3                      
#> 4                      
#> 5