- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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
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
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"
- Related Articles
- How to find all combinations of a vector elements without space in R?
- Extract string vector elements up to a fixed number of characters in R.
- How to find the intersection of elements in a string vector in R?
- How to find unique permutations if a vector contains repeated elements in R?
- How to convert the repeated elements of strings in a vector to unique elements in R?
- How to create a large vector with repetitive elements of varying size in R?
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- How to find the frequency of repeated and unique values in a vector in R?
- How to replace one vector elements with another vector elements in R?
- How to find the count of a particular character in a string vector in R?
- How to find the number of unique values in a vector by excluding missing values in R?
- How to create a list of an unordered combination of elements in a string vector in R?
- Print all possible combinations of r elements in a given array of size n in C++
- How to find the rank of a vector elements in R from largest to smallest?
- Find the count of unique group combinations in an R data frame.

Advertisements