icombinations: Iterator that generates all combinations of a vector taken m...

Description Usage Arguments Details Value Examples

Description

Constructs an iterator generates all combinations of a vector taken m at a time. This function is similar to combn.

Usage

1
icombinations(object, m, replacement = FALSE)

Arguments

object

vector

m

the length of each combination

replacement

Generate combinations with replacement? Default: no.

Details

By default, the combinations are **without replacement** so that elements are not repeated. To generate combinations **with replacement**, set replacement=TRUE.

The function implementation is loosely based on the combinations function from Python's itertools. Combinations with replacement are based on combinations_with_replacement from the same Python library.

Value

iterator that generates all combinations of object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Combinations without replacement
it <- icombinations(1:4, m=2)

iterators::nextElem(it) # c(1, 2)
iterators::nextElem(it) # c(1, 3)
iterators::nextElem(it) # c(1, 4)
iterators::nextElem(it) # c(2, 3)
iterators::nextElem(it) # c(2, 4)
iterators::nextElem(it) # c(3, 4)

# Combinations without replacement
it <- icombinations(1:4, m=2, replacement=TRUE)

iterators::nextElem(it) # c(1, 1)
iterators::nextElem(it) # c(1, 2)
iterators::nextElem(it) # c(1, 3)
iterators::nextElem(it) # c(1, 4)
iterators::nextElem(it) # c(2, 2)
iterators::nextElem(it) # c(2, 3)
iterators::nextElem(it) # c(2, 4)
iterators::nextElem(it) # c(3, 3)
iterators::nextElem(it) # c(3, 4)
iterators::nextElem(it) # c(4, 4)

it3 <- icombinations(1:5, m=2)
as.list(it3)
utils::combn(x=1:5, m=2, simplify=FALSE)

ramhiser/itertools2 documentation built on May 26, 2019, 10:09 p.m.