Find the count of unique group combinations in an R data frame.


To find the count of unique 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 three grouping columns say G1, G2, and G3 then we can count the unique group combinations in df by using the below given command −

count(df,G1,G2,G3)%%ungroup()

Example 1

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)
df1<-data.frame(Grp1,Grp2,Grp3)
df1

The following dataframe is created

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

To load dplyr package and count the unique group combinations in df1 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)
df1<-data.frame(Grp1,Grp2,Grp3)
library(dplyr)
count(df1,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   2
3 1    2   1   1
4 1    2   2   1
5 2    1   1   4
6 2    1   2   5
7 2    2   1   3
8 2    2   2   2

Example 2

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)
df2<-data.frame(Class1,Class2,Class3)
df2

The following dataframe is created

  Class1  Class2 Class3
 1 First  Second Second
 2 Second Third  Second
 3 Third  Second Third
 4 First  Third  Second
 5 Second Third  First
 6 Second Third  First
 7 First  Second Second
 8 Third  First  Third
 9 Third  Third  Third
10 Second First  Third
11 Third  Second Second
12 Second Second Second
13 Third  Second Second
14 Third  First  Third
15 First  First  First
16 Third  Third  Third
17 Third  Third  Third
18 First  Third  Third
19 Third  Second First
20 Second Second Second

To count the unique group combinations in df2 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)
df2<-data.frame(Class1,Class2,Class3)
count(df2,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 1
 2 First  Second Second 2
 3 First  Third  Second 1
 4 First  Third  Third 1
 5 Second First  Third 1
 6 Second Second Second 2
 7 Second Third  First 2
 8 Second Third  Second 1
 9 Third  First  Third 2
10 Third  Second First 1
11 Third  Second Second 2
12 Third  Second Third 1
13 Third  Third  Third 3

Updated on: 10-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements