Skip to contents

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.

Usage

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_dbl(), fmt_header_n(), fmt_int(), fmt_n_percent(), fmt_pct()

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

setosa
(N=50)

versicolor
(N=50)

virginica
(N=50)

Sepal.Length

Mean (SD)

5.0 (0.4)

5.9 (0.5)

6.6 (0.6)

Median (IQR)

5.0 (0.4)

5.9 (0.7)

6.5 (0.7)

Range

4.3 - 5.8

4.9 - 7.0

4.9 - 7.9

Sepal.Width

Mean (SD)

3.4 (0.4)

2.8 (0.3)

3.0 (0.3)

Median (IQR)

3.4 (0.5)

2.8 (0.5)

3.0 (0.4)

Range

2.3 - 4.4

2.0 - 3.4

2.2 - 3.8

Petal.Length

Mean (SD)

1.5 (0.2)

4.3 (0.5)

5.6 (0.6)

Median (IQR)

1.5 (0.2)

4.3 (0.6)

5.5 (0.8)

Range

1.0 - 1.9

3.0 - 5.1

4.5 - 6.9

Petal.Width

Mean (SD)

0.2 (0.1)

1.3 (0.2)

2.0 (0.3)

Median (IQR)

0.2 (0.1)

1.3 (0.3)

2.0 (0.5)

Range

0.1 - 0.6

1.0 - 1.8

1.4 - 2.5