To create contingency table in R there are two different ways to do it.

The following methods show how you can do it with syntax.

Method 1: Use table() Function

table(df$column1, df$column2)

Method 2: Use tapply() Function

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

The following example shows how to create a contigency table in R using two different approaches.

Create a Contigency Table Using table() Function in R

Let’s see how we can use table() function to create 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

Here the output shows frequency of values of Machine_name and Status in the data frame.

Create a Contigency Table Using Using tapply() Function in R

Let’s see how we can use tapply() function in to create 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 

The output shows contigency table which having frequency of values of Machine_name and Status column of data frame.