How to extract unique combination of rows in an R data frame?


To extract the unique combination of rows, we can subset the data frame with single square brackets and use the negation of the duplicated function after sorting the rows in the data frame. The sorting of the data frame can be done with the help of apply function and we will have to transpose the sorting as shown in the below examples. To understand how it works, do it in parts.

Example1

Consider the below data frame −

Live Demo

> x1<-rpois(20,1)
> x2<-rpois(20,1)
> df1<-data.frame(x1,x2)
> df1

Output

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

Extracting the unique combination of rows in df1 −

> df1[!duplicated(t(apply(df1,1,sort))),]

Output

   x1 x2
1   3  1
2   1  0
5   2  2
6   3  0
7   4  2
8   3  3
11  0  0
12  0  2
13  1  1
14  4  3

Example2

Live Demo

> y1<-sample(LETTERS[1:3],20,replace=TRUE)
> y2<-sample(LETTERS[1:3],20,replace=TRUE)
> df2<-data.frame(y1,y2)
> df2

Output

   y1 y2
1   B  B
2   C  C
3   C  B
4   A  B
5   A  C
6   C  B
7   C  B
8   B  C
9   C  B
10  A  A
11  A  A
12  A  B
13  B  B
14  B  C
15  B  C
16  B  C
17  C  A
18  C  B
19  B  C
20  C  B

Extracting the unique combination of rows in df2 −

> df2[!duplicated(t(apply(df2,1,sort))),]

Output

   y1 y2
1   B  B
2   C  C
3   C  B
4   A  B
5   A  C
10  A  A

Updated on: 05-Mar-2021

606 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements