Cross Tables

A cross table provides information about a bivariate distribution, that is, the distribution of two variables. To obtain a cross table, we just need to add a second variable in the table() function: Here, we display a cross table between satisfaction with democracy and gender:

table(
  pss$stfdem, 
  pss$gndr
)
##     
##      female male
##   0     117  109
##   1     133  135
##   2     211  225
##   3     287  331
##   4     377  377
##   5     466  384
##   6     311  320
##   7     251  271
##   8     164  174
##   9      82   97
##   10     41   42
Which variable is the column variable?

As with everything in R, we can also save this table as an object:

mytable <- table(
  pss$stfdem, 
  pss$gndr
)

To display percentages instead of frequencies, the prop.table() function is used. This function can output column, row, or total percentages. We can either call a table object or use the table() function within the function. Here, we call the row percentage by specifying the value \(1\) in the last argument.

# Zeilenprozentuierung
prop.table(
  mytable, 
  1
) 
##     
##         female      male
##   0  0.5176991 0.4823009
##   1  0.4962687 0.5037313
##   2  0.4839450 0.5160550
##   3  0.4644013 0.5355987
##   4  0.5000000 0.5000000
##   5  0.5482353 0.4517647
##   6  0.4928685 0.5071315
##   7  0.4808429 0.5191571
##   8  0.4852071 0.5147929
##   9  0.4581006 0.5418994
##   10 0.4939759 0.5060241
# alternativer Weg mit table()-Funktion
prop.table(
  table(
    pss$stfdem, 
    pss$gndr
  ), 
  1
)

The column percentages can be displayed by changing the second argument to the value \(2\):

prop.table(
  mytable,
  2
)
##     
##          female       male
##   0  0.04795082 0.04421907
##   1  0.05450820 0.05476673
##   2  0.08647541 0.09127789
##   3  0.11762295 0.13427992
##   4  0.15450820 0.15294118
##   5  0.19098361 0.15578093
##   6  0.12745902 0.12981744
##   7  0.10286885 0.10993915
##   8  0.06721311 0.07058824
##   9  0.03360656 0.03935091
##   10 0.01680328 0.01703854

If no second argument is provided, the total percentages will be automatically displayed:

prop.table(mytable)
##     
##           female        male
##   0  0.023853211 0.022222222
##   1  0.027115189 0.027522936
##   2  0.043017329 0.045871560
##   3  0.058511723 0.067482161
##   4  0.076860347 0.076860347
##   5  0.095005097 0.078287462
##   6  0.063404689 0.065239551
##   7  0.051172273 0.055249745
##   8  0.033435270 0.035474006
##   9  0.016717635 0.019775739
##   10 0.008358818 0.008562691

Marginal Frequencies

The marginal frequencies can be displayed using the margin.table() function. Again, the value \(1\) in the second argument represents row marginal frequencies, and the value \(2\) displays the column marginal frequencies:

# Zeilenrandhäufigkeiten
margin.table(
  mytable, 
  1
)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
## 226 268 436 618 754 850 631 522 338 179  83
# Spaltenrandhäufigkeiten
margin.table(
  mytable, 
  2
)
## 
## female   male 
##   2440   2465

Other Libraries

There are various libraries that allow for a more aesthetically pleasing representation of relative frequencies. One of them is the library gmodels:

if(!require("gmodels")) install.packages("gmodels")
library("gmodels")

From the library, we use the CrossTable() function. This function outputs all the information mentioned above at once:

CrossTable(
  pss$stfdem, 
  pss$gndr
)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  4905 
## 
##  
##              | pss$gndr 
##   pss$stfdem |    female |      male | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       117 |       109 |       226 | 
##              |     0.186 |     0.184 |           | 
##              |     0.518 |     0.482 |     0.046 | 
##              |     0.048 |     0.044 |           | 
##              |     0.024 |     0.022 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       133 |       135 |       268 | 
##              |     0.001 |     0.001 |           | 
##              |     0.496 |     0.504 |     0.055 | 
##              |     0.055 |     0.055 |           | 
##              |     0.027 |     0.028 |           | 
## -------------|-----------|-----------|-----------|
##            2 |       211 |       225 |       436 | 
##              |     0.160 |     0.158 |           | 
##              |     0.484 |     0.516 |     0.089 | 
##              |     0.086 |     0.091 |           | 
##              |     0.043 |     0.046 |           | 
## -------------|-----------|-----------|-----------|
##            3 |       287 |       331 |       618 | 
##              |     1.357 |     1.343 |           | 
##              |     0.464 |     0.536 |     0.126 | 
##              |     0.118 |     0.134 |           | 
##              |     0.059 |     0.067 |           | 
## -------------|-----------|-----------|-----------|
##            4 |       377 |       377 |       754 | 
##              |     0.010 |     0.010 |           | 
##              |     0.500 |     0.500 |     0.154 | 
##              |     0.155 |     0.153 |           | 
##              |     0.077 |     0.077 |           | 
## -------------|-----------|-----------|-----------|
##            5 |       466 |       384 |       850 | 
##              |     4.407 |     4.362 |           | 
##              |     0.548 |     0.452 |     0.173 | 
##              |     0.191 |     0.156 |           | 
##              |     0.095 |     0.078 |           | 
## -------------|-----------|-----------|-----------|
##            6 |       311 |       320 |       631 | 
##              |     0.027 |     0.026 |           | 
##              |     0.493 |     0.507 |     0.129 | 
##              |     0.127 |     0.130 |           | 
##              |     0.063 |     0.065 |           | 
## -------------|-----------|-----------|-----------|
##            7 |       251 |       271 |       522 | 
##              |     0.289 |     0.287 |           | 
##              |     0.481 |     0.519 |     0.106 | 
##              |     0.103 |     0.110 |           | 
##              |     0.051 |     0.055 |           | 
## -------------|-----------|-----------|-----------|
##            8 |       164 |       174 |       338 | 
##              |     0.102 |     0.101 |           | 
##              |     0.485 |     0.515 |     0.069 | 
##              |     0.067 |     0.071 |           | 
##              |     0.033 |     0.035 |           | 
## -------------|-----------|-----------|-----------|
##            9 |        82 |        97 |       179 | 
##              |     0.557 |     0.552 |           | 
##              |     0.458 |     0.542 |     0.036 | 
##              |     0.034 |     0.039 |           | 
##              |     0.017 |     0.020 |           | 
## -------------|-----------|-----------|-----------|
##           10 |        41 |        42 |        83 | 
##              |     0.002 |     0.002 |           | 
##              |     0.494 |     0.506 |     0.017 | 
##              |     0.017 |     0.017 |           | 
##              |     0.008 |     0.009 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |      2440 |      2465 |      4905 | 
##              |     0.497 |     0.503 |           | 
## -------------|-----------|-----------|-----------|
## 
##