Find the unique pair combinations of an R data frame column values.

R ProgrammingServer Side ProgrammingProgramming

To find the unique pair combinations of an R data frame column values, we can use combn function along with unique function.

For Example, if we have a data frame called df that contains a column say x then we can find the unique pair combinations of all column values by using the command given below −

combn(unique(df$x),2,FUN=paste,collapse=' ')

Example 1

Following snippet creates a sample data frame −

Grp<-sample(c("I","II","III"),20,replace=TRUE)
df1<-data.frame(Grp)
df1

The following dataframe is created

Grp
1 II
2 III
3 I
4 I
5 II
6 I
7 II
8 III
9 III
10 I
11 I
12 I
13 I
14 II
15 III
16 II
17 I
18 II
19 II
20 III

To find unique pair combinations for values in column Grp of df1 on the above created data frame, add the following code to the above snippet −

Grp<-sample(c("I","II","III"),20,replace=TRUE)
df1<-data.frame(Grp)
combn(unique(df1$Grp),2,FUN=paste,collapse=' ')

Output

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

[1] "II III" "II I" "III I"

Example 2

Following snippet creates a sample data frame −

Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE)
df2<-data.frame(Class)
df2

The following dataframe is created

Class
1 Second
2 Fourth
3 Fourth
4 Second
5 Fourth
6 Third
7 Fourth
8 Third
9 First
10 Fifth
11 Second
12 Second
13 Third
14 Second
15 First
16 Second
17 Fourth
18 First
19 Fifth
20 First

To find unique pair combinations for values in column Class of df2 on the above created data frame, add the following code to the above snippet −

Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE)
df2<-data.frame(Class)
combn(unique(df2$Class),2,FUN=paste,collapse=' ')

Output

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

[1] "Second Fourth" "Second Third" "Second First" "Second Fifth"
[5] "Fourth Third" "Fourth First" "Fourth Fifth" "Third First"
[9] "Third Fifth" "First Fifth"

Example 3

Following snippet creates a sample data frame −

Category<-sample(c("Extra Small","Small","Medium","Large","Extra Large"),20,replace=TRUE)
df3<-data.frame(Category)
df3

The following dataframe is created

Category
1 Large
2 Extra Small
3 Extra Small
4 Small
5 Large
6 Extra Small
7 Medium
8 Large
9 Large
10 Extra Large
11 Extra Small
12 Extra Small
13 Extra Small
14 Extra Large
15 Large
16 Extra Small
17 Large
18 Medium
19 Extra Large
20 Extra Large

To find unique pair combinations for values in column Category of df3 on the above created data frame, add the following code to the above snippet −

Category<-sample(c("Extra Small","Small","Medium","Large","Extra Large"),20,replace=TRUE)
df3<-data.frame(Category)
combn(unique(df3$Category),2,FUN=paste,collapse=' ')

Output

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

[1] "Large Extra Small" "Large Small"
[3] "Large Medium" "Large Extra Large"
[5] "Extra Small Small" "Extra Small Medium"
[7] "Extra Small Extra Large" "Small Medium"
[9] "Small Extra Large" "Medium Extra Large"
raja
Updated on 03-Nov-2021 05:48:46

Advertisements