Contingency Tables in R Programming

Prerequisite: Data Structures in R Programming

Contingency tables are very useful to condense a large number of observations into smaller to make it easier to maintain tables. A contingency table shows the distribution of a variable in the rows and another in its columns. Contingency tables are not only useful for condensing data, but they also show the relations between variables. They are a way of summarizing categorical variables. A contingency table that deals with a single table are called a complex or a flat contingency table.

Making Contingency tables

A contingency table is a way to redraw data and assemble it into a table. And, it shows the layout of the original data in a manner that allows the reader to gain an overall summary of the original data. The table() function is used in R to create a contingency table. The table() function is one of the most versatile functions in R. It can take any data structure as an argument and turn it into a table. The more complex the original data, the more complex is the resulting contingency table.

Creating contingency tables from Vectors

In R a vector is an ordered collection of basic data types of a given length. The only key thing here is all the elements of a vector must be of the identical data type e.g homogenous data structures. Vectors are one-dimensional data structures. It is the simplest data object from which you can create a contingency table.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# Contingency Table
  
# Creating a vector
vec = c(2, 4, 3, 1, 6, 3, 2, 1, 4, 5)
  
# Creating contingency table from vec using table()
conTable = table(vec)
print(conTable)

chevron_right


Output:

vec
1 2 3 4 5 6 
2 2 2 2 1 1 

In the given program what happens is first when we execute table() command on the vector it sorts the vector value and also prints the frequencies of every element given in the vector.

Creating contingency tables from Data

Now we will see a simple example that provides a data frame containing character values in one column and also containing a factor in one of its columns. This one column of factors contains character variables. In order to create our contingency table from data, we will make use of the table(). In the following example, the table() function returns a contingency table. Basically, it returns a tabular result of the categorical variables.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# Contingency Table
  
# Creating a data frame
df = data.frame( 
  "Name" = c("Amiya", "Rosy", "Asish"), 
  "Gender" = c("Male", "Female", "Male")
  
# Creating contingency table from data using table()
conTable = table(df)
print(conTable)

chevron_right


Output:

           Gender
 Name    Female Male
 Amiya      0    1
 Asish      0    1
 Rosy       1    0

Creating custom contingency tables

The contingency table in R can be created using only a part of the data which is in contrast with collecting data from all the rows and columns. We can create a custom contingency table in R using the following ways:

  • Using Columns of a Data Frame in a Contingency Table
  • Using Rows of a Data Frame in a Contingency Table
  • By Rotating Data Frames in R
  • Creating Contingency Tables from Matrix Objects in R