A function that creates tables

create_table <- function(dataset, var){
  table(dataset[var]) |>
    knitr::kable()
}

The function above uses the table() function to create frequency tables, and then this gets passed to the knitr::kable() function that produces a good looking table for our rendered document:

create_table(mtcars, "am")
am Freq
0 19
1 13

Let’ suppose that we want to generate a document that would look like this:

So it would look like this:

Frequency table for variable: “am”

create_table(mtcars, "am")
am Freq
0 19
1 13

We don’t want to create these sections for every variable by hand.

Instead, we can define a function that returns the R markdown code required to create this. This is this function:

return_section <- function(dataset, var){
  a <- knitr::knit_expand(text = c("## Frequency table for variable: {{variable}}",   
                                   create_table(dataset, var)),
                          variable = var)
  cat(a, sep = "\n")
}

This new function, return_section() uses knitr::knit_expand() to generate R Markdown code. Words between {{}} get replaced by the provided var argument to the function. So when we call return_section("am"), {{variable}} is replaced by "am". "am" then gets passed down to create_table() and the frequency table gets generated. We can now generate all the section by simply applying our function to a list of column names:

invisible(lapply(colnames(mtcars), return_section, dataset = mtcars))

Frequency table for variable: mpg

mpg Freq
10.4 2
13.3 1
14.3 1
14.7 1
15 1
15.2 2
15.5 1
15.8 1
16.4 1
17.3 1
17.8 1
18.1 1
18.7 1
19.2 2
19.7 1
21 2
21.4 2
21.5 1
22.8 2
24.4 1
26 1
27.3 1
30.4 2
32.4 1
33.9 1

Frequency table for variable: cyl

cyl Freq
4 11
6 7
8 14

Frequency table for variable: disp

disp Freq
71.1 1
75.7 1
78.7 1
79 1
95.1 1
108 1
120.1 1
120.3 1
121 1
140.8 1
145 1
146.7 1
160 2
167.6 2
225 1
258 1
275.8 3
301 1
304 1
318 1
350 1
351 1
360 2
400 1
440 1
460 1
472 1

Frequency table for variable: hp

hp Freq
52 1
62 1
65 1
66 2
91 1
93 1
95 1
97 1
105 1
109 1
110 3
113 1
123 2
150 2
175 3
180 3
205 1
215 1
230 1
245 2
264 1
335 1

Frequency table for variable: drat

drat Freq
2.76 2
2.93 1
3 1
3.07 3
3.08 2
3.15 2
3.21 1
3.23 1
3.54 1
3.62 1
3.69 1
3.7 1
3.73 1
3.77 1
3.85 1
3.9 2
3.92 3
4.08 2
4.11 1
4.22 2
4.43 1
4.93 1

Frequency table for variable: wt

wt Freq
1.513 1
1.615 1
1.835 1
1.935 1
2.14 1
2.2 1
2.32 1
2.465 1
2.62 1
2.77 1
2.78 1
2.875 1
3.15 1
3.17 1
3.19 1
3.215 1
3.435 1
3.44 3
3.46 1
3.52 1
3.57 2
3.73 1
3.78 1
3.84 1
3.845 1
4.07 1
5.25 1
5.345 1
5.424 1

Frequency table for variable: qsec

qsec Freq
14.5 1
14.6 1
15.41 1
15.5 1
15.84 1
16.46 1
16.7 1
16.87 1
16.9 1
17.02 2
17.05 1
17.3 1
17.4 1
17.42 1
17.6 1
17.82 1
17.98 1
18 1
18.3 1
18.52 1
18.6 1
18.61 1
18.9 2
19.44 1
19.47 1
19.9 1
20 1
20.01 1
20.22 1
22.9 1

Frequency table for variable: vs

vs Freq
0 18
1 14

Frequency table for variable: am

am Freq
0 19
1 13

Frequency table for variable: gear

gear Freq
3 15
4 12
5 5

Frequency table for variable: carb

carb Freq
1 7
2 10
3 3
4 10
6 1
8 1