How to find the unique combinations of a string vector elements with a fixed size in R?


A unique combination of vector elements can be found by using combn function with unique function and size argument will help us to identify the size of each of combination. For example, if we have a vector containing string values defined as x then the unique combinations of vector elements of size 2 can be created by using combn(unique(x),2).

Example1

 Live Demo

x1<−c("A","B","C","A","D","E","A","C")
combn(unique(x1),2)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"
[2,] "B" "C" "D" "E" "C" "D" "E" "D" "E" "E"

Example

combn(unique(x1),3)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A" "A" "A" "A" "A" "A" "B" "B" "B" "C"
[2,] "B" "B" "B" "C" "C" "D" "C" "C" "D" "D"
[3,] "C" "D" "E" "D" "E" "E" "D" "E" "E" "E"

Example

combn(unique(x1),4)

Output

[,1] [,2] [,3] [,4] [,5]
[1,] "A" "A" "A" "A" "B"
[2,] "B" "B" "B" "C" "C"
[3,] "C" "C" "D" "D" "D"
[4,] "D" "E" "E" "E" "E"

Example

combn(unique(x1),5)

Output

    [,1]
[1,] "A"
[2,] "B"
[3,] "C"
[4,] "D"
[5,] "E"

Example2

 Live Demo

x2<−c("India","Russia","India","USA","USA","UK","Egypt","UK")
combn(unique(x2),2)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "India" "India" "India" "India" "Russia" "Russia" "Russia" "USA" "USA"
[2,] "Russia" "USA" "UK" "Egypt" "USA" "UK" "Egypt" "UK" "Egypt"
    [,10]
[1,] "UK"
[2,] "Egypt"

Example

combn(unique(x2),3)

Output

     [,1]     [,2]     [,3]   [,4]     [,5]   [,6]    [,7]     [,8]
[1,] "India" "India" "India" "India" "India" "India" "Russia" "Russia"
[2,] "Russia" "Russia" "Russia" "USA" "USA"   "UK"    "USA"    "USA"
[3,] "USA"    "UK"     "Egypt"  "UK"  "Egypt"  "Egypt" "UK" "Egypt"
      [,9] [,10]
[1,] "Russia" "USA"
[2,] "UK"     "UK"
[3,] "Egypt" "Egypt"

Example

combn(unique(x2),4)

Output

     [,1]  [,2] [,3] [,4] [,5]
[1,] "India" "India" "India" "India" "Russia"
[2,] "Russia" "Russia" "Russia" "USA" "USA"
[3,] "USA" "USA" "UK" "UK" "UK"
[4,] "UK" "Egypt" "Egypt" "Egypt" "Egypt"

Example

combn(unique(x2),5)

Output

      [,1]
[1,] "India"
[2,] "Russia"
[3,] "USA"
[4,] "UK"
[5,] "Egypt"

Example3

 Live Demo

x3<−c("G1","G2","G3","G1","G2","G4","G5","G3","G5","G2")
combn(unique(x3),2)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "G1" "G1" "G1" "G1" "G2" "G2" "G2" "G3" "G3" "G4"
[2,] "G2" "G3" "G4" "G5" "G3" "G4" "G5" "G4" "G5" "G5"

Example

combn(unique(x3),3)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "G1" "G1" "G1" "G1" "G1" "G1" "G2" "G2" "G2" "G3"
[2,] "G2" "G2" "G2" "G3" "G3" "G4" "G3" "G3" "G4" "G4"
[3,] "G3" "G4" "G5" "G4" "G5" "G5" "G4" "G5" "G5" "G5"

Example

combn(unique(x3),4)

Output

[,1] [,2] [,3] [,4] [,5]
[1,] "G1" "G1" "G1" "G1" "G2"
[2,] "G2" "G2" "G2" "G3" "G3"
[3,] "G3" "G3" "G4" "G4" "G4"
[4,] "G4" "G5" "G5" "G5" "G5"

Example

combn(unique(x3),5)

Output

     [,1]
[1,] "G1"
[2,] "G2"
[3,] "G3"
[4,] "G4"
[5,] "G5"

Example4

 Live Demo

x4<−c("HOT","COLD","SWEET","BITTER","HOT","SWEET")
combn(unique(x4),2)

Output

combn(unique(x4),2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "HOT" "HOT" "HOT" "COLD" "COLD" "SWEET"
[2,] "COLD" "SWEET" "BITTER" "SWEET" "BITTER" "BITTER"

Example

combn(unique(x4),3)

Output

[,1] [,2] [,3] [,4]
[1,] "HOT" "HOT" "HOT" "COLD"
[2,] "COLD" "COLD" "SWEET" "SWEET"
[3,] "SWEET" "BITTER" "BITTER" "BITTER"

Example

combn(unique(x4),4)

Output

      [,1]
[1,] "HOT"
[2,] "COLD"
[3,] "SWEET"
[4,] "BITTER"

Updated on: 05-Feb-2021

928 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements