icombinations: Combinations iterator

Description Usage Arguments Format Details See Also Examples

View source: R/combinations.R

Description

This function returns a Combinations iterator for iterating combinations of k items from n items. The iterator allows users to fetch the next combination(s) via the getnext() method.

Usage

1
2
3
4
Combinations

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

Arguments

x

an integer or a vector, will be treated as n if integer; otherwise, will be treated as v. Should not be specified together with n and v.

k

an integer, the number of items drawn, defaults to n if freq is NULL else sum(freq)

n

an integer, the total number of items, its value may be implicitly deduced from length(v) or length(freq)

v

a vector to be drawn, defaults to 1:n.

freq

an integer vector of item repeat frequencies

replace

an logical to draw items with replacement

skip

the number of combinations skipped

Format

An object of class R6ClassGenerator of length 25.

Details

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

1
Combinations$new(n, k, v = NULL, freq = NULL, replace = FALSE)
1
2
3
getnext(d = 1L, layout = NULL, drop = NULL)
collect(layout = "row")
reset()
d

number of fetched arrangements

layout

if "row", "column" or "list" 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
icomb <- icombinations(5, 2)
icomb$getnext()
icomb$getnext(2)
icomb$getnext(layout = "column", drop = FALSE)
# collect remaining combinations
icomb$collect()

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

Example output

[1] 1 2
     [,1] [,2]
[1,]    1    3
[2,]    1    4
     [,1]
[1,]    1
[2,]    5
     [,1] [,2]
[1,]    2    3
[2,]    2    4
[3,]    2    5
[4,]    3    4
[5,]    3    5
[6,]    4    5
 [1] 3 4 5 6 5 6 7 7 8 9

arrangements documentation built on Sept. 13, 2020, 5:20 p.m.