- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We can use permn function from combinat package to find the permutations but if we have repeated elements in the vector then the result will not have unique permutations, therefore, we need to use unique function along with the permn function. For example, if we have a vector 1, 2, 1 then the permutations will be (1 2 1), (1 1 2), (1 1 2), (1 2 1), (2 1 1), (2 1 1) and the unique permutations will be (1 2 1), (1 1 2), (2 1 1).

x1<-c(1,2,1,2) x1

[1] 1 2 1 2

Finding all permutations −

permn(x1)

[[1]] [1] 1 2 1 2 [[2]] [1] 1 2 2 1 [[3]] [1] 1 2 2 1 [[4]] [1] 2 1 2 1 [[5]] [1] 2 1 1 2 [[6]] [1] 1 2 1 2 [[7]] [1] 1 1 2 2 [[8]] [1] 1 1 2 2 [[9]] [1] 1 1 2 2 [[10]] [1] 1 1 2 2 [[11]] [1] 1 2 1 2 [[12]] [1] 2 1 1 2 [[13]] [1] 2 1 2 1 [[14]] [1] 1 2 2 1 [[15]] [1] 1 2 2 1 [[16]] [1] 1 2 1 2 [[17]] [1] 2 1 1 2 [[18]] [1] 2 1 2 1 [[19]] [1] 2 2 1 1 [[20]] [1] 2 2 1 1 [[21]] [1] 2 2 1 1 [[22]] [1] 2 2 1 1 [[23]] [1] 2 1 2 1 [[24]] [1] 2 1 1 2

Finding the unique permutations −

unique(permn(x1))

[[1]] [1] 1 2 1 2 [[2]] [1] 1 2 2 1 [[3]] [1] 2 1 2 1 [[4]] [1] 2 1 1 2 [[5]] [1] 1 1 2 2 [[6]] [1] 2 2 1 1

x2<-c(0,1,0) permn(x2)

[[1]] [1] 0 1 0 [[2]] [1] 0 0 1 [[3]] [1] 0 0 1 [[4]] [1] 0 1 0 [[5]] [1] 1 0 0 [[6]] [1] 1 0 0

unique(permn(x2))

[[1]] [1] 0 1 0 [[2]] [1] 0 0 1 [[3]] [1] 1 0 0

- Related Questions & Answers
- How to convert the repeated elements of strings in a vector to unique elements in R?
- How to find the frequency of repeated and unique values in a vector in R?
- How to find the cumulative sums if a vector contains NA values in R?
- How to create a frequency table of a vector that contains repeated values in R?
- How to check if a vector contains a given value in R?
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- How to find the unique combinations of a string vector elements with a fixed size in R?
- Check if list contains all unique elements in Python
- How to create a vector with repeated values in R?
- How to remove the first replicate in a vector using another vector that contains similar elements in R?
- How to find the frequency vector elements that exists in another vector in R?
- How to replace one vector elements with another vector elements in R?
- How to find the unique elements in multiple vectors in R?
- How to find the intersection of elements in a string vector in R?
- How to find all combinations of a vector elements without space in R?

Advertisements