Open In App

Contingency Tables in R Programming

Improve
Improve
Like Article
Like
Save
Share
Report

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 homogeneous data structures. Vectors are one-dimensional data structures. It is the simplest data object from which you can create a contingency table.

Example:  

Python3




# 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)


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:  

Python3




# 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)


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
     
  • Using Columns of a Data Frame in a Contingency Table: With the help of table() command, we are able to specify the columns with which the contingency tables can be created. In order to do so, you only need to pass the name of vector objects in the parameter of table() command. 
    Example:

Python3




# 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 by selecting a column
conTable = table(df$Name)
print(conTable)


Output: 

Amiya Asish  Rosy 
    1     1     1 

From the output, you can notice that the table() command sorts the name in alphabetical order along with their frequencies of occurrence.

  • Using Rows of a Data Frame in a Contingency Table: We can’t create a contingency table using rows of a data frame directly as we did in “using column” part. With the help of the matrix, we can create a contingency table by looking at the rows of a data frame.
    Example:

Python3




# 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 by selecting rows
conTable = table(as.matrix(df[2:3, ]))
print(conTable)


Output: 

Asish Female   Male   Rosy 
  1      1      1      1 
  • By Rotating Data Frames in R: We can also create a contingency table by rotating a data frame in R. We can perform a rotation of the data, that is, transpose of the data using the t() command.
    Example:

Python3




# R program to illustrate
# Contingency Table
 
# Creating a data frame
df = data.frame(
  "Name" = c("Amiya", "Rosy", "Asish"),
  "Gender" = c("Male", "Female", "Male")
)
 
# Rotating the data frame
newDf = t(df)
 
# Creating contingency table by rotating data frame
conTable = table(newDf)
print(conTable)


Output: 

newDf
Amiya  Asish Female   Male   Rosy 
  1      1      1      2      1 
  • Creating Contingency Tables from Matrix Objects in R A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. Matrices are two-dimensional, homogeneous data structures. We can create a contingency table by using this matrix object.
    Example:

Python3




# R program to illustrate
# Contingency Table
 
# Creating a matrix
A = matrix(
  c(1, 2, 4, 1, 5, 6, 2, 4, 7),
  nrow = 3,
  ncol = 3
)
 
# Creating contingency table using matrix object
conTable = table(A)
print(conTable)


Output: 

A
1 2 4 5 6 7 
2 2 2 1 1 1 

Converting Objects into tables

As mentioned above, a table is a special type of data object which is similar to the matrix but also possesses several differences.

  • Converting Matrix Objects into tables: We can directly convert a matrix object into table by using the as.table() command. Just pass the matrix object as a parameter to the as.table() command.
    Example:

Python3




# R program to illustrate
# Contingency Table
 
# Creating a matrix
A = matrix(
  c(1, 2, 4, 1, 5, 6, 2, 4, 7),
  nrow = 3,
  ncol = 3
)
 
# Converting Matrix Objects into tables
newTable = as.table(A)
print(newTable)


Output: 

  A B C
A 1 1 2
B 2 5 4
C 4 6 7
  • Converting Data frame Objects into tables: We can’t directly convert a data frame object into table by using the as.table() command. In the case of a data frame, the object can be converted into the matrix, and then it can be converted into the table using as.table() command. 
    Example:

Python3




# R program to illustrate
# Contingency Table
 
# Creating a data frame
df = data.frame(
  "Name" = c("Amiya", "Rosy", "Asish"),
  "Gender" = c("Male", "Female", "Male")
)
 
# Converting data frame object to matrix object
modifiedDf = as.matrix(df)
 
# Converting Matrix Objects into tables
newTable = as.table(modifiedDf)
print(newTable)


Output: 

  Name  Gender
A Amiya Male  
B Rosy  Female
C Asish Male 

 



Last Updated : 28 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads