iterpc: Efficient Iterator for Permutations and Combinations

Description Usage Arguments Value Examples

View source: R/iterpc.R

Description

Initialize a iterator for permutations or combinations

Usage

1

Arguments

n

the length of the input sequence or a vector of frequencies for a multiset.

r

the length of the output sequence. If missing, equals to sum(n).

labels

if missing, natural numbers are used unless n is a table object. In that case, the names of n are used.

ordered

TRUE corresponds to permutation and FALSE corresponds to combinations.

replace

with/without replacement. Default is FALSE.

Value

a permutation/combination iterator

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#1) all combinations of drawing 2 items from {1, 2, 3}
I <- iterpc(5, 2)
getall(I)

#2) continuing 1), get combination by combination
I <- iterpc(5, 2)
getnext(I) # return 1,2
getnext(I) # return 1,3
getnext(I, 2) # return next 2 results

#3) 3) all permutations of {1, 2, 3} and use of labels
I <- iterpc(3, labels=c("a", "b", "c"), ordered=TRUE)
getall(I)

#4) permutations of multiset and
I <- iterpc(c(2, 1, 1), labels=c("a", "b", "c"), ordered=TRUE)
getall(I)

#5) combinations with replacement and the use of table as input
x <- c("a","a","b","c")
I <- iterpc(table(x), 3, replace=TRUE)
getall(I)

Example output

      [,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] 1 2
[1] 1 3
     [,1] [,2]
[1,]    1    4
[2,]    1    5
     [,1] [,2] [,3]
[1,] "a"  "b"  "c" 
[2,] "a"  "c"  "b" 
[3,] "b"  "a"  "c" 
[4,] "b"  "c"  "a" 
[5,] "c"  "a"  "b" 
[6,] "c"  "b"  "a" 
      [,1] [,2] [,3] [,4]
 [1,] "a"  "a"  "b"  "c" 
 [2,] "a"  "a"  "c"  "b" 
 [3,] "a"  "b"  "a"  "c" 
 [4,] "a"  "b"  "c"  "a" 
 [5,] "a"  "c"  "a"  "b" 
 [6,] "a"  "c"  "b"  "a" 
 [7,] "b"  "a"  "a"  "c" 
 [8,] "b"  "a"  "c"  "a" 
 [9,] "b"  "c"  "a"  "a" 
[10,] "c"  "a"  "a"  "b" 
[11,] "c"  "a"  "b"  "a" 
[12,] "c"  "b"  "a"  "a" 
      [,1] [,2] [,3]
 [1,] "a"  "a"  "a" 
 [2,] "a"  "a"  "b" 
 [3,] "a"  "a"  "c" 
 [4,] "a"  "b"  "b" 
 [5,] "a"  "b"  "c" 
 [6,] "a"  "c"  "c" 
 [7,] "b"  "b"  "b" 
 [8,] "b"  "b"  "c" 
 [9,] "b"  "c"  "c" 
[10,] "c"  "c"  "c" 

iterpc documentation built on April 24, 2018, 9:03 a.m.