Learn R Programming

arrangements (version 1.0.0)

Combinations: Combinations iterator

Description

This function returns a Combinations iterator which allows users to fetch the next combination(s) via the getnext() method. All remaing combinations of the iterator can be fetched via the collect() method.

Usage

Combinations

icombinations(n, k, x = NULL, freq = NULL, replace = FALSE)

Arguments

n

an integer, would be determined implicitly from x or f if missing

k

an integer

x

an optional vector indicating item labels

freq

an integer vector of item repeat frequencies

replace

an logical to draw items with replacement

Format

An object of class R6ClassGenerator of length 24.

Details

The Combinations class can be initialized by using the convenient wrapper icombinations or

Combinations$new(n, k, x = NULL, freq = NULL, replace = FALSE)

getnext(d = 1L, type = NULL, drop = d == 1L && is.null(type))
collect(type = "r")
reset()
d

number of fetched arrangements

type

if "r", "c" or "l" is specified, the returned value would be a "row-major" matrix, a "column-major" matrix or a list respectively

drop

vectorize a matrix or unlist a list

See Also

combinations for generating all combinations and ncombinations to calculate number of combinations

Examples

Run this code
# NOT RUN {
icomb <- icombinations(5, 2)
icomb$getnext()
icomb$getnext(2)
icomb$getnext(type = "c", drop = FALSE)
# collect remaining combinations
icomb$collect()

library(foreach)
foreach(x = icombinations(5, 2), .combine=c) %do% {
  sum(x)
}
# }

Run the code above in your browser using DataLab