Skip to content

Data Science Tutorials

For Data Science Learners

  • Lottery Prediction Using Machine Learning
    Lottery Prediction Using Machine Learning Machine Learning
  • Statistical test assumptions and requirements
    Statistical test assumptions and requirements Statistics
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE R
  • Correlation Coefficient p value in R
    Correlation Coefficient p value in R R
  • How to perform TBATS Model in R
    How to perform TBATS Model in R R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • How do augmented analytics work
    How do augmented analytics work? R
How to create contingency tables in R

How to create contingency tables in R?

Posted on May 3May 12 By Admin No Comments on How to create contingency tables in R?

Create contingency tables in R, Contingency tables are helpful for condensing a huge number of observations into smaller, more manageable tables.

We’ll learn about contingency tables and how to make them in this R tutorial. Complex/flat tables, cross-tabulation, and recovering original data from contingency tables will all be covered.

As you can see, this course will be jam-packed with information. So, without further ado, let’s get this party started.

What are Contingency Tables?

Contingency tables are helpful for condensing a huge number of observations into smaller, more manageable tables.

We’ll learn about contingency tables and how to make them in this R tutorial. Complex/flat tables, cross-tabulation, and recovering original data from contingency tables will all be covered.

As you can see, this course will be jam-packed with information. So, without further ado, let’s get this party started.

To make a contingency table in R, use the table() function. One of R’s most versatile functions is the table function. It can convert any data structure into a table as an argument.

Take a look at the following example:

ct1 <- table(mtcars$gear, mtcars$cyl, dnn=c("gears","cylinders"))
ct1
cylinders
gears  4  6  8
    3  1  2 12
    4  8  4  0
    5  2  1  2

We use two categorical variables from the mtcars datasets in the example above. The number of gears and cylinders within the autos are the two factors.

The number of gears is listed as a row, while the number of cylinders is listed as a column in the resulting table.

Calculate the row totals of a contingency table

The margin.table() function can be used to calculate the totals of each row in a contingency table. Let’s have a look at an example of this:

margin.table(ct1, margin = 1)
gears
 3  4  5
15 12  5

Calculate the column totals of a contingency table

Using the margin.table() function, we can calculate the totals of each of the columns in a contingency table in a similar way. Only the margin argument needs to be changed to 2. Here’s an example of what I’m talking about.

margin.table(ct1, margin = 2)
cylinders
 4  6  8
11  7 14

The function addmargins

Another technique to determine the sum totals of the rows and columns of a contingency table is to use the addmargins() function.

The totals of all the rows and columns of the input contingency table are found using this function. Let’s have a look at a practical application of this function.

addmargins(ct1)
cylinders
gears  4  6  8 Sum
  3    1  2 12  15
  4    8  4  0  12
  5    2  1  2   5
  Sum 11  7 14  32

Proportional contingency tables.

We can find the proportional weight of each value in a contingency table using the prop.tables() function. This is exemplified in the following example.

prop.table(ct1)
cylinders
gears       4       6       8
    3 0.03125 0.06250 0.37500
    4 0.25000 0.12500 0.00000
    5 0.06250 0.03125 0.06250

Proportionate rows are used to create contingency tables

We can also determine the row proportions in a contingency table bypassing margin = 1 as an input to the prop.table() function.

Detecting and Dealing with Outliers: First Step

prop.table(ct1, margin = 1)
  cylinders
gears          4          6          8
    3 0.06666667 0.13333333 0.80000000
    4 0.66666667 0.33333333 0.00000000
    5 0.40000000 0.20000000 0.40000000

Proportional columns in R contingency tables

Using margin = 2 in the prop.table() function’s inputs, we may obtain the column proportions in a contingency table.

prop.table(ct1, margin = 2)
cylinders
gears          4          6          8
    3 0.09090909 0.28571429 0.85714286
    4 0.72727273 0.57142857 0.00000000
    5 0.18181818 0.14285714 0.14285714

Creating Flat Contingency tables in R

The ftable() function in R can be used to build simple or elaborate contingency tables. Let’s look at this in more detail using the following example.

ft1 <- ftable(mtcars[c("gear","vs","am","cyl")])
ft1
  cyl  4  6  8
gear vs am            
3    0  0       0  0 12
        1       0  0  0
     1  0       1  2  0
        1       0  0  0
4    0  0       0  0  0
        1       0  2  0
     1  0       2  2  0
        1       6  0  0
5    0  0       0  0  0
        1       1  1  2
     1  0       0  0  0
        1       1  0  0

Cross Tabulation and The xtabs Function

Using R’s xtabs() function, we can generate a cross-tabulation contingency table. The function returns an object with the “table” and “xtabs” classes. Here’s an example of how to use the xtabs function.

c1 <- sample(letters[1:4],16,replace = TRUE)
c2 <- sample(LETTERS[1:4],16,replace = TRUE)
df1 <- data.frame(c1,c2)
t1 <- table(df1$c1,df1$c2)
t2 <- as.data.frame.matrix(t1)
xt1 <- xtabs(A~B+C,t2)
xt1
  C
B   0 1 2
  0 0 0 3
  1 0 0 0
  2 2 0 0

Recovering data from contingency tables in R

The as.data.frame() function can be used to retrieve data from contingency tables prepared with the xtabs() function. A data frame object is the end product.

df2 <- as.data.frame(xt1)
df2
B C Freq
1 0 0    0
2 1 0    0
3 2 0    2
4 0 1    0
5 1 1    0
6 2 1    0
7 0 2    3
8 1 2    0
9 2 2    0

Summary

Contingency tables are a useful tool for summarising data and identifying relationships and dependencies among variables. It’s a method of presenting data in a compressed format.

We learned what contingency tables are in this R tutorial. We looked at how to make contingency tables in R and how to use them to do things like add along their margins and calculate proportionate values.

In addition, we learned about flat contingency tables and how to make them in R.

Finally, we discovered cross-tabulation and how to extract data from a contingency table.

R

Post navigation

Previous Post: Methods for Integrating R and Hadoop complete Guide
Next Post: Best online course for R programming

Related Posts

  • Export output as text in R R
  • Divide data into groups in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • KPSS test in R
    KPSS Test in R With Examples R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • optim Function in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Gamma distribution in R
    Gamma distribution in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Select variables of data frame in R R
  • How to use image function in R
    How to use the image function in R R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • How to Replace String in Column in R
    How to Replace String in Column using R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme