How to create permutations as a list in R?

R ProgrammingServer Side ProgrammingProgramming

The permutation is the combination with orders. For example, if we want to create a key for lock with a sequence of numbers then it must be order in some direction, otherwise, it will be difficult to remember and easy to unlock. We can find the permutation of some numbers or characters by using permn function of combinat package.

Loading the combinat package −

library(combinat)

Examples that create list of permutations −

permn(LETTERS[1:4])
[[1]]
[1] "A" "B" "C" "D"
[[2]]
[1] "A" "B" "D" "C"
[[3]]
[1] "A" "D" "B" "C"
[[4]]
[1] "D" "A" "B" "C"
[[5]]
[1] "D" "A" "C" "B"
[[6]]
[1] "A" "D" "C" "B"
[[7]]
[1] "A" "C" "D" "B"
[[8]]
[1] "A" "C" "B" "D"
[[9]]
[1] "C" "A" "B" "D"
[[10]]
[1] "C" "A" "D" "B"
[[11]]
[1] "C" "D" "A" "B"
[[12]]
[1] "D" "C" "A" "B"
[[13]]
[1] "D" "C" "B" "A"
[[14]]
[1] "C" "D" "B" "A"
[[15]]
[1] "C" "B" "D" "A"
[[16]]
[1] "C" "B" "A" "D"
[[17]]
[1] "B" "C" "A" "D"
[[18]]
[1] "B" "C" "D" "A"
[[19]]
[1] "B" "D" "C" "A"
[[20]]
[1] "D" "B" "C" "A"
[[21]]
[1] "D" "B" "A" "C"
[[22]]
[1] "B" "D" "A" "C"
[[23]]
[1] "B" "A" "D" "C"
[[24]]
[1] "B" "A" "C" "D"
permn(c("Aluminium","Calcium","Carbon","Nitrogen"))
[[1]]
[1] "Aluminium" "Calcium" "Carbon" "Nitrogen"
[[2]]
[1] "Aluminium" "Calcium" "Nitrogen" "Carbon"
[[3]]
[1] "Aluminium" "Nitrogen" "Calcium" "Carbon"
[[4]]
[1] "Nitrogen" "Aluminium" "Calcium" "Carbon"
[[5]]
[1] "Nitrogen" "Aluminium" "Carbon" "Calcium"
[[6]]
[1] "Aluminium" "Nitrogen" "Carbon" "Calcium"
[[7]]
[1] "Aluminium" "Carbon" "Nitrogen" "Calcium"
[[8]]
[1] "Aluminium" "Carbon" "Calcium" "Nitrogen"
[[9]]
[1] "Carbon" "Aluminium" "Calcium" "Nitrogen"
[[10]]
[1] "Carbon" "Aluminium" "Nitrogen" "Calcium"
[[11]]
[1] "Carbon" "Nitrogen" "Aluminium" "Calcium"
[[12]]
[1] "Nitrogen" "Carbon" "Aluminium" "Calcium"
[[13]]
[1] "Nitrogen" "Carbon" "Calcium" "Aluminium"
[[14]]
[1] "Carbon" "Nitrogen" "Calcium" "Aluminium"
[[15]]
[1] "Carbon" "Calcium" "Nitrogen" "Aluminium"
[[16]]
[1] "Carbon" "Calcium" "Aluminium" "Nitrogen"
[[17]]
[1] "Calcium" "Carbon" "Aluminium" "Nitrogen"
[[18]]
[1] "Calcium" "Carbon" "Nitrogen" "Aluminium"
[[19]]
[1] "Calcium" "Nitrogen" "Carbon" "Aluminium"
[[20]]
[1] "Nitrogen" "Calcium" "Carbon" "Aluminium"
[[21]]
[1] "Nitrogen" "Calcium" "Aluminium" "Carbon"
[[22]]
[1] "Calcium" "Nitrogen" "Aluminium" "Carbon"
[[23]]
[1] "Calcium" "Aluminium" "Nitrogen" "Carbon"
[[24]]
[1] "Calcium" "Aluminium" "Carbon" "Nitrogen"
permn(c(1,2,3))
[[1]]
[1] 1 2 3
[[2]]
[1] 1 3 2
[[3]]
[1] 3 1 2
[[4]]
[1] 3 2 1
[[5]]
[1] 2 3 1
[[6]]
[1] 2 1 3
raja
Published on 21-Aug-2020 10:12:04
Advertisements