Find the frequency of exclusive group combinations based on multiple categorical columns in R.


To find the frequency of exclusive group combinations in an R data frame, we can use count function of dplyr package along with ungroup function.

For Example, if we have a data frame called df that contains four grouping columns say Grp1, Grp2, Grp3, and Grp4 then we can count the unique group combinations in df by using the below command −

count(df,Grp1,Grp2,Grp3,Grp4)%%ungroup()

Example 1

Following snippet creates a sample data frame −

Class1<-sample(c("First","Second","Third"),20,replace=TRUE)
Class2<-sample(c("First","Second","Third"),20,replace=TRUE)
Class3<-sample(c("First","Second","Third"),20,replace=TRUE)
Score<-sample(1:50,20)
df1<-data.frame(Class1,Class2,Class3,Score)
df1

Output

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

  Class1 Class2 Class3 Score
1    Third  First Third 40
2    First  First Second 38
3    First Second Third 25
4    First Second Second 2
5    First  Third Third 12
6    First Second First 13
7   Second  Third Third 31
8    First  First First 15
9    First  Third Third 43
10  Second  First Second 28
11   First First Third 22
12   Third  Third First 50
13   First Second Second 39
14   First  First First 41
15  Second  Third Third 49
16  Second  First First 36
17   Third  Third First 20
18  Second Second Second 19
19   First  Third First 5
20  Second  First Third 47

To load dplyr package and find the frequency of exclusive group combinations for groups Class1, Class2, and Class3 on the above created data frame, add the following code to the above snippet −

Class1<-sample(c("First","Second","Third"),20,replace=TRUE)
Class2<-sample(c("First","Second","Third"),20,replace=TRUE)
Class3<-sample(c("First","Second","Third"),20,replace=TRUE)
Score<-sample(1:50,20)
df1<-data.frame(Class1,Class2,Class3,Score)
library(dplyr)
count(df1,Class1,Class2,Class3)%%ungroup()

Output

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

   Class1 Class2 Class3 n
1   First  First First 2
2   First  First Second 1
3   First  First Third 1
4   First Second First 1
5   First Second Second 2
6   First Second Third 1
7   First  Third First 1
8   First  Third Third 2
9  Second  First First 1
10 Second  First Second 1
11 Second  First Third 1
12 Second Second Second 1
13 Second  Third Third 2
14  Third  First Third 1
15  Third  Third First 2

Example 2

Following snippet creates a sample data frame −

Grp1<-sample(1:2,20,replace=TRUE)
Grp2<-sample(1:2,20,replace=TRUE)
Grp3<-sample(1:2,20,replace=TRUE)
df2<-data.frame(Grp1,Grp2,Grp3)
df2

The following dataframe is created

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

To find the frequency of exclusive group combinations for groups Grp1, Grp2, and Grp3 on the above created data frame, add the following code to the above snippet −

Grp1<-sample(1:2,20,replace=TRUE)
Grp2<-sample(1:2,20,replace=TRUE)
Grp3<-sample(1:2,20,replace=TRUE)
df2<-data.frame(Grp1,Grp2,Grp3)
count(df2,Grp1,Grp2,Grp3)%%ungroup()

Output

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

   Grp1 Grp2 Grp3 n
1   1    1     1  2
2   1    1     2  3
3   1    2     1  1
4   1    2     2  3
5   2    1     1  4
6   2    1     2  1
7   2    2     1  1
8   2    2     2  5

Updated on: 02-Nov-2021

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements