Examples of generating combinations

Randy Lai

2020-01-08

  1. combinations: without replacement: distinct items
I <- iterpc(5, 2)
getall(I)
##       [,1] [,2]
##  [1,]    1    2
##  [2,]    1    3
##  [3,]    1    4
##  [4,]    1    5
##  [5,]    2    3
##  [6,]    2    4
##  [7,]    2    5
##  [8,]    3    4
##  [9,]    3    5
## [10,]    4    5
  1. combinations: with replacement: distinct items
I <- iterpc(5, 2, replace = TRUE)
getall(I)
##       [,1] [,2]
##  [1,]    1    1
##  [2,]    1    2
##  [3,]    1    3
##  [4,]    1    4
##  [5,]    1    5
##  [6,]    2    2
##  [7,]    2    3
##  [8,]    2    4
##  [9,]    2    5
## [10,]    3    3
## [11,]    3    4
## [12,]    3    5
## [13,]    4    4
## [14,]    4    5
## [15,]    5    5
  1. combinations: without replacement: non distinct items
x <- c("a", "a", "b", "c")
I <- iterpc(table(x), 2)
# or I <- iterpc(c(2,1,1), 2, label=c("a", "b", "c"))
getall(I)
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    2
## [3,]    1    3
## [4,]    2    3
  1. combinations: with replacement: non distinct items
x <- c("a", "a", "b", "c")
I <- iterpc(table(x), 2, replace = TRUE)
# or I = iterpc(c(2,1,1), 2, label=c("a", "b", "c"), replace=TRUE)
getall(I)
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    2
## [3,]    1    3
## [4,]    2    2
## [5,]    2    3
## [6,]    3    3