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.
icombinations(
x = NULL,
k = NULL,
n = NULL,
v = NULL,
freq = NULL,
replace = FALSE,
skip = NULL
)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.
an integer, the number of items drawn, defaults to n if freq is NULL else sum(freq)
an integer, the total number of items, its value may be implicitly deduced from length(v) or length(freq)
a vector to be drawn, defaults to 1:n.
an integer vector of item repeat frequencies
an logical to draw items with replacement
the number of combinations skipped
The Combinations class can be initialized by using the convenient wrapper icombinations or
Combinations$new(n, k, v = NULL, freq = NULL, replace = FALSE)
getnext(d = 1L, layout = NULL, drop = NULL)
collect(layout = "row")
reset()number of fetched arrangements
if "row", "column" or "list" is specified, the returned value would be a "row-major" matrix, a "column-major" matrix or a list respectively
vectorize a matrix or unlist a list
combinations for generating all combinations and ncombinations to calculate number of combinations
arrangements::abstractiter -> arrangements::iter -> arrangements::Arrangements -> Combinations
Inherited methods
new()Combinations$new(n, k, v = NULL, freq = NULL, replace = FALSE, skip = NULL)print()icomb <- icombinations(5, 2)
icomb$getnext()
#> [1] 1 2
icomb$getnext(2)
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 1 4
icomb$getnext(layout = "column", drop = FALSE)
#> [,1]
#> [1,] 1
#> [2,] 5
# collect remaining combinations
icomb$collect()
#> [,1] [,2]
#> [1,] 2 3
#> [2,] 2 4
#> [3,] 2 5
#> [4,] 3 4
#> [5,] 3 5
#> [6,] 4 5
library(foreach)
#> Error in library(foreach): there is no package called ‘foreach’
foreach(x = icombinations(5, 2), .combine=c) %do% {
sum(x)
}
#> Error in foreach(x = icombinations(5, 2), .combine = c) %do% { sum(x)}: could not find function "%do%"