To create a contingency table in R, there are two different ways to do it. A contingency table is a type of table in a matrix format that displays the frequency distribution of the variables.

In this article, we will explore how to create a contingency table in R using two different methods.

Method 1: Use table() Function

The table() function in R is used to create a contingency table.

Here’s the syntax:

table(df$column1, df$column2)

Method 2: Use tapply() Function

The tapply() function can also be used to create a contingency table.

Here’s the syntax:

tapply(df$column1, df$column2, table)

The following examples show how to create a contingency table in R using these two approaches.

Create a Contingency Table Using table() Function in R

Let’s see how we can use the table() function to create a contingency table in R:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
                 Pressure=c(12.39,11.25,12.15,13.48,13.78,11.12,12.21,12.58,9.6,8.85,7.89,9.63,12.36,11.45,9.47,8.12),
                 Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Create Contingency table 
s <- table(df$Machine_name, df$Status)

# Print Contingency table
print(s)

Output: 👇️

    OK Suspect
  A  3       0
  B  1       2
  C  2       0
  D  1       1
  E  0       2
  F  0       1
  G  0       1
  H  2       0

In this example, we use the table() function to create a contingency table from the specified columns, displaying the frequency of values of Machine_name and Status in the dataframe.

Create a Contingency Table Using tapply() Function in R

Let’s see how we can use the tapply() function to create a contingency table in R:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
                 Pressure=c(12.39,11.25,12.15,13.48,13.78,11.12,12.21,12.58,9.6,8.85,7.89,9.63,12.36,11.45,9.47,8.12),
                 Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Create Contingency table 
s <- tapply(df$Machine_name, df$Status, table)

# Print Contingency table
print(s)

Output: 👇️

$OK

A B C D H 
3 1 2 1 2 

$Suspect

B D E F G 
2 1 2 1 1 

In this example, we use the tapply() function to create a contingency table, showing the frequency of values of Machine_name and Status columns of the dataframe.