Learn R Programming

iterpc (version 0.2.4)

iterpc: Iterator for Permutations and Combinations

Description

Iterator for Permutations and Combinations

Initialize a iterator for permutations or combinations

Usage

iterpc(n, r = NULL, labels = NULL, ordered = FALSE, replace = FALSE)

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 corresponses to permutation and FALSE corresponses to combinations.
replace
with/without replacement. Default is FALSE.

Value

  • a permutation/combination iterator

Examples

Run this code
#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 = 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)

Run the code above in your browser using DataLab