Examples of generating permutations

Randy Lai

2020-01-08

  1. permutations: without replacement: distinct items
I <- iterpc(5, 2, ordered = TRUE)
getall(I)
##       [,1] [,2]
##  [1,]    1    2
##  [2,]    1    3
##  [3,]    1    4
##  [4,]    1    5
##  [5,]    2    1
##  [6,]    2    3
##  [7,]    2    4
##  [8,]    2    5
##  [9,]    3    1
## [10,]    3    2
## [11,]    3    4
## [12,]    3    5
## [13,]    4    1
## [14,]    4    2
## [15,]    4    3
## [16,]    4    5
## [17,]    5    1
## [18,]    5    2
## [19,]    5    3
## [20,]    5    4
  1. ermutations: with replacement: distinct items
I <- iterpc(5, 2, replace = TRUE, ordered = TRUE)
getall(I)
##       [,1] [,2]
##  [1,]    1    1
##  [2,]    1    2
##  [3,]    1    3
##  [4,]    1    4
##  [5,]    1    5
##  [6,]    2    1
##  [7,]    2    2
##  [8,]    2    3
##  [9,]    2    4
## [10,]    2    5
## [11,]    3    1
## [12,]    3    2
## [13,]    3    3
## [14,]    3    4
## [15,]    3    5
## [16,]    4    1
## [17,]    4    2
## [18,]    4    3
## [19,]    4    4
## [20,]    4    5
## [21,]    5    1
## [22,]    5    2
## [23,]    5    3
## [24,]    5    4
## [25,]    5    5
  1. permutations: without replacement: non distinct items
x <- c("a", "a", "b", "c")
I <- iterpc(table(x), 2, ordered = TRUE)
# or I <- iterpc(c(2,1,1), 2, label=c("a", "b", "c"), ordered = TRUE)
getall(I)
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    2
## [3,]    1    3
## [4,]    2    1
## [5,]    2    3
## [6,]    3    1
## [7,]    3    2
  1. permutations: with replacement: non distinct items
x <- c("a", "a", "b", "c")
I <- iterpc(table(x), 2, replace = TRUE, ordered = TRUE)
# or I = iterpc(c(2,1,1), 2, label=c("a", "b", "c"), replace= T RUE, ordered = TRUE)
getall(I)
##       [,1] [,2]
##  [1,]    1    1
##  [2,]    1    2
##  [3,]    1    3
##  [4,]    2    1
##  [5,]    2    2
##  [6,]    2    3
##  [7,]    3    1
##  [8,]    3    2
##  [9,]    3    3

mirror server hosted at Truenetwork, Russian Federation.