10  Tables for 2 or More Variables

This chapter is a companion to Chapter 9, which explains how to graph two and three variables.

If you create bar graphs, you will want to create tables. While graphs are excellent for providing the overview of relationships, tables allow a reader to look at details.

To create tables you will use the sjtab command from the sjPlot package. We will look at an example of a two variable table and a three variable table.

10.1 A two variable table using categorical variables

We will use the data from graph examples in sections 9.1.3 and 9.1.4. As before we will recode the variable to collapse 4 categories for the population of the town into four categories.

ethiopia_small$G_TOWNSIZE2_recode <- fct_collapse(ethiopia_small$G_TOWNSIZE2, 
   "Under 5,000" = c("Under 5,000"), 
   "5,000-20,000" = c("5000-20000"),
   "20,000-100,000" = c("20000-100000"),
   "100,000 and more" = c("100000-500000", "500000 and more")
)

Next we will create the code for the table. We add a row percentages using “show.row.prc”.

ethiopia_small |>
  select(G_TOWNSIZE2_recode, E1_LITERACY) |>
  sjtab(fun = "xtab", 
        show.col.prc = TRUE)
Settlement size_5
groups
Respondent´s
literacy
Total
Literate Illiterate
Under 5,000 105
12.7 %
91
22.5 %
196
15.9 %
5,000-20,000 411
49.8 %
234
57.9 %
645
52.4 %
20,000-100,000 280
33.9 %
77
19.1 %
357
29 %
100,000 and more 30
3.6 %
2
0.5 %
32
2.6 %
Total 826
100 %
404
100 %
1230
100 %
χ2=50.686 · df=3 · Cramer's V=0.203 · p=0.000

10.2 Adding a third variable to a table using categorical variables

We will use the data from graph examples in sections 9.1.4. We will use “group_by” to add a third variable. In this instance we will use Q260, the sex of the respondent.

ethiopia_small |>
  group_by(Q260) |>
  select(G_TOWNSIZE2_recode, E1_LITERACY) |>
  sjtab(fun = "xtab", 
        show.col.prc = TRUE)
Adding missing grouping variables: `Q260`
Sex: Female
Settlement size_5
groups
Respondent´s
literacy
Total
Literate Illiterate
Under 5,000 58
12.6 %
43
26.4 %
101
16.2 %
5,000-20,000 240
52.3 %
91
55.8 %
331
53.2 %
20,000-100,000 144
31.4 %
28
17.2 %
172
27.7 %
100,000 and more 17
3.7 %
1
0.6 %
18
2.9 %
Total 459
100 %
163
100 %
622
100 %
χ2=27.010 · df=3 · Cramer's V=0.208 · p=0.000

 

Sex: Male
Settlement size_5
groups
Respondent´s
literacy
Total
Literate Illiterate
Under 5,000 47
12.8 %
48
19.9 %
95
15.6 %
5,000-20,000 171
46.6 %
143
59.3 %
314
51.6 %
20,000-100,000 136
37.1 %
49
20.3 %
185
30.4 %
100,000 and more 13
3.5 %
1
0.4 %
14
2.3 %
Total 367
100 %
241
100 %
608
100 %
χ2=28.833 · df=3 · Cramer's V=0.218 · p=0.000