Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case 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
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
How to create contingency tables in R

How to create contingency tables in R?

Posted on May 3May 12 By Jim No Comments on How to create contingency tables in R?
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
R

Post navigation

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

Related Posts

  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • Difference between R and Python
    Difference between R and Python R
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • How to Count Distinct Values in R
    How to Count Distinct Values in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • Predictive Modeling and Data Science
    Predictive Modeling and Data Science Machine Learning
  • How To Become a Business Intelligence Analyst
    How To Become a Business Intelligence Analyst Course
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • Filtering for Unique Values
    Filtering for Unique Values in R- Using the dplyr R
  • Radar plot in R
    How to create Radar Plot in R-ggradar R
  • glm function in R
    glm function in r-Generalized Linear Models R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme