How to add row percentages to contingency table in R?

R ProgrammingServer Side ProgrammingProgramming

To add row percentage to contingency table in R, we can use rowSums and sum function with table values and combine them with cbind function.

For Example, if we have a table called TAB then we can add row percentages to TAB by using the below command −

cbind(TAB,rowSums(TAB),rowSums(TAB)/sum(TAB))

Example 1

Following snippet creates a sample data frame −

Grp1<-sample(LETTERS[1:5],20,replace=TRUE)
Grp2<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(Grp1,Grp2)
df1

The following dataframe is created

Grp1 Grp2
1  A  b
2  D  d
3  A  c
4  A  b
5  D  a
6  D  a
7  A  b
8  B  b
9  A  d
10 A  d
11 B  d
12 B  d
13 E  e
14 C  b
15 E  b
16 E  d
17 C  c
18 E  c
19 E  e
20 E  d

To create contingency table based on columns Grp1 and Grp2 on the above created data frame, add the following code to the above snippet −

Grp1<-sample(LETTERS[1:5],20,replace=TRUE)
Grp2<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(Grp1,Grp2)
C_Table1<-table(df1$Grp1,df1$Grp2)
C_Table1

If you execute all the above given snippets as a single program, it generates the following Output −

  a b c d e
A 0 3 1 2 0
B 0 1 0 2 0
C 0 1 1 0 0
D 2 0 0 1 0
E 0 1 1 2 2

To find row percentages to C_Table1 on the above created data frame, add the following code to the above snippet −

Grp1<-sample(LETTERS[1:5],20,replace=TRUE)
Grp2<-sample(letters[1:5],20,replace=TRUE)
df1<-data.frame(Grp1,Grp2)
C_Table1<-table(df1$Grp1,df1$Grp2)
cbind(C_Table1,rowSums(C_Table1),rowSums(C_Table1)/sum(C_Table1))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  a b c d e
A 0 3 1 2 0 6 0.30
B 0 1 0 2 0 3 0.15
C 0 1 1 0 0 2 0.10
D 2 0 0 1 0 3 0.15
E 0 1 1 2 2 6 0.30

Example 2

Following snippet creates a sample data frame −

f1<-sample(1:4,20,replace=TRUE)
f2<-sample(c(5,10,15,20),20,replace=TRUE)
df2<-data.frame(f1,f2)
df2

The following dataframe is created

  f1 f2
1  1 20
2  2  5
3  4  5
4  3 20
5  1 15
6  2  5
7  1 10
8  4  5
9  1  5
10 1 10
11 2 15
12 3 20
13 4  5
14 1 10
15 4  5
16 2 10
17 2 10
18 2 15
19 1  5
20 4 15

To create contingency table based on columns f1 and f2 on the above created data frame, add the following code to the above snippet −

f1<-sample(1:4,20,replace=TRUE)
f2<-sample(c(5,10,15,20),20,replace=TRUE)
df2<-data.frame(f1,f2)
C_Table2<-table(df2$f1,df2$f2)
C_Table2

If you execute all the above given snippets as a single program, it generates the following Output −

  5 10 15 20
1 2  3  1  1
2 2  2  2  0
3 0  0  0  2
4 4  0  1  0

To find row percentages to C_Table2 on the above created data frame, add the following code to the above snippet −

f1<-sample(1:4,20,replace=TRUE)
f2<-sample(c(5,10,15,20),20,replace=TRUE)
df2<-data.frame(f1,f2)
C_Table2<-table(df2$f1,df2$f2)
cbind(C_Table2,rowSums(C_Table2),rowSums(C_Table2)/sum(C_Table2))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  5 10 15 20
1 2  3  1  1 7 0.35
2 2  2  2  0 6 0.30
3 0  0  0  2 2 0.10
4 4  0  1  0 5 0.25
raja
Published on 05-Nov-2021 07:31:44
Advertisements