Skip to contents

This function computes a one or two way contingency table and creates a flextable from the result.

The function is largely inspired by "PROC FREQ" from "SAS" and was written with the intent to make it as compact as possible.

Usage

proc_freq(
  x,
  row = character(),
  col = character(),
  include.row_percent = TRUE,
  include.column_percent = TRUE,
  include.table_percent = TRUE,
  include.table_count = TRUE,
  weight = character(),
  ...
)

Arguments

x

a data.frame object containing variable(s) to use for counts.

row

characer column names for row

col

characer column names for column

include.row_percent

boolean whether to include the row percents; defaults to TRUE

include.column_percent

boolean whether to include the column percents; defaults to TRUE

include.table_percent

boolean whether to include the table percents; defaults to TRUE

include.table_count

boolean whether to include the table counts; defaults to TRUE

weight

character column name for weight

...

unused arguments

Examples

proc_freq(mtcars, "vs", "gear")

vs

gear

3

4

5

Total

0

Count

12 (37.5%)

2 (6.2%)

4 (12.5%)

18 (56.2%)

Mar. pct (1)

80.0% ; 66.7%

16.7% ; 11.1%

80.0% ; 22.2%

1

Count

3 (9.4%)

10 (31.2%)

1 (3.1%)

14 (43.8%)

Mar. pct

20.0% ; 21.4%

83.3% ; 71.4%

20.0% ; 7.1%

Total

Count

15 (46.9%)

12 (37.5%)

5 (15.6%)

32 (100.0%)

(1) Columns and rows percentages

proc_freq(mtcars, "gear", "vs", weight = "wt")

gear

vs

0

1

Total

3

Count

49 (47.8%)

9 (8.9%)

58 (56.7%)

Mar. pct (1)

74.2% ; 84.3%

25.0% ; 15.7%

4

Count

5 (5.3%)

26 (25.2%)

31 (30.5%)

Mar. pct

8.3% ; 17.5%

70.9% ; 82.5%

5

Count

12 (11.3%)

2 (1.5%)

13 (12.8%)

Mar. pct

17.5% ; 88.5%

4.1% ; 11.5%

Total

Count

66 (64.5%)

37 (35.5%)

103 (100.0%)

(1) Columns and rows percentages