The table() function uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels. The table() method creates a categorical representation of data with the variable names and the frequency in a table.
Contingency Table in R
The contingency table is a built-in data structure used to redraw data and assemble it into a table. It shows the distribution of a variable in the rows and another in its columns.
Contingency tables are a way of summarizing categorical variables. It deals with a single table is called a complex or a flat contingency table.
The table() function takes any data structure as an argument and creates a contingency table. The more complex the original data, the more complex is the resulting contingency table.
data <- c(21, 46, 30, 19, 18, 11)
contigency_table <- table(data)
print(contigency_table)
Output
data
11 18 19 21 30 46
1 1 1 1 1 1
In this example, we are executing the table() function to a vector.
Creating contingency tables from a data frame
Data Frame in R is a table or two-dimensional array-like structure in which a row contains a set of values, and each column holds values of one variable.
Let’s create a data frame using data.frame() function and pass that into a table() function.
data <- data.frame(
"Parents" = c("Shobhanaben", "Devendrabhai"),
"Gender" = c("Female", "Male")
)
contigency_table <- table(data)
print(contigency_table)
Output
Gender
Parents Female Male
Devendrabhai 0 1
Shobhanaben 1 0
Creating custom contingency tables in R
To create a custom contingency table, use one of the following approaches.
- By Rotating Data Frames in R.
- Using Columns of a Data Frame in a Contingency Table.
- Creating Contingency Tables from Matrix Objects in R.
- Using Rows of a Data Frame in a Contingency Table.
Rotating data frames in R
To rotate or transpose data in R, use the t() function.
data <- data.frame(
"Parents" = c("Shobhanaben", "Devendrabhai"),
"Gender" = c("Female", "Male")
)
transpose <- t(data)
contigency_table <- table(transpose)
print(contigency_table)
Output
transpose
Devendrabhai Female Male Shobhanaben
1 1 1 1
Using Columns of a Data Frame
Using the table() function, we can specify the columns with which the contingency tables can be created.
data <- data.frame(
"Parents" = c("Shobhanaben", "Devendrabhai"),
"Gender" = c("Female", "Male")
)
contigency_table <- table(data$Parents)
print(contigency_table)
Output
Devendrabhai Shobhanaben
1 1
You can see from the output that it returns the output in alphabetical order.
Using Matrix Objects in R
A matrix is a rectangular form of numbers in rows and columns. Thus, the matrix is a two-dimensional, homogeneous data structure.
mat <- matrix(
c(19, 21, 46, 11),
nrow = 2,
ncol = 2
)
table <- table(mat)
print(table)
Output
mat
11 19 21 46
1 1 1 1
Using Rows of a Data Frame
We can’t build a contingency table using rows of a data frame directly as we did in the “using column” section. However, with the help of the matrix, we can create a contingency table by looking at the rows of a data frame.
data <- data.frame(
"Parents" = c("Shobhanaben", "Devendrabhai"),
"Gender" = c("Female", "Male")
)
contigency_table <- table(as.matrix(data[2:3, ]))
print(contigency_table)
Output
Devendrabhai Male
1 1
That’s it for this tutorial.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.