How to create a contingency table with sum on the margins from an R data frame?


The sum of rows and columns on the margins in a contingency table are always useful because they are used for different type of calculations such as odds ratio, probability etc. If an R data frame has factor columns then we can create a contingency table for that data frame and it can be done by using addmargins function.

Example

Consider the below data frame −

 Live Demo

x1<-sample(LETTERS[1:4],20,replace=TRUE)
x2<-sample(c("India","USA","China"),20,replace=TRUE)
df1<-data.frame(x1,x2)
df1

Output

   x1 x2
1 B China
2 B India
3 B India
4 D India
5 B India
6 D   USA
7 D   USA
8 A   USA
9 D   USA
10 C India
11 B China
12 D China
13 D China
14 A India
15 D   USA
16 A China
17 D India
18 A China
19 B China
20 A India

Creating contingency table for x1 and x2 with sum on the margins −

Example

>CT1<-addmargins(table(df1$x1,df1$x2),c(1,2)) 
>CT1

Output

  China India USA Sum
A   2    2     1   5
B   3    3     0   6
C   0    1     0   1
D   2    2     4   8
Sum 7    8     5  20

Let’s have a look at another example −

Example

 Live Demo

y1<-sample(c("John","Christina","Michael","Sona"),20,replace=TRUE)
y2<-sample(c("1","2","3","4"),20,replace=TRUE)
df2<-data.frame(y1,y2)
df2

Output

    y1        y2
1 Sona        2
2 John        3
3 John        4
4 Sona        2
5 Christina   4
6 Michael     2
7 Michael     4
8 John        1
9 John        4
10 John       1
11 Michael    3
12 Sona       1
13 Sona       2
14 Michael    2
15 Michael    2
16 Michael    1
17 Michael    3
18 Christina  1
19 Christina  4
20 Sona       1

Creating contingency table for y1 and y2 with sum on the margins −

Example

>CT2<-addmargins(table(df2$y1,df2$y2),c(1,2)) 
>CT2

Output

          1  2  3  4  Sum
Christina 1  0  0  2   3
John      2  0  1  2   5
Michael   1  3  2  1   7
Sona      2  3  0  0   5
Sum       6  6  3  5  20

Updated on: 08-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements