combinations: Combinations generator

Description Usage Arguments See Also Examples

View source: R/combinations.R

Description

This function generates all the combinations of selecting k items from n items. The results are in lexicographical order.

Usage

1
2
3
combinations(x = NULL, k = NULL, n = NULL, v = NULL, freq = NULL,
  replace = FALSE, layout = NULL, nitem = -1L, skip = NULL,
  index = NULL, nsample = NULL, drop = 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

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

nitem

number of combinations required, usually used with skip

skip

the number of combinations skipped

index

a vector of indices of the desired combinations

nsample

sampling random combinations

drop

vectorize a matrix or unlist a list

See Also

icombinations for iterating combinations and ncombinations to calculate number of combinations

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
28
29
30
# choose 2 from 4
combinations(4, 2)
combinations(LETTERS[1:3], k = 2)

# multiset with frequencies c(2, 3)
combinations(k = 3, freq = c(2, 3))

# with replacement
combinations(4, 2, replace = TRUE)

# column major
combinations(4, 2, layout = "column")

# list output
combinations(4, 2, layout = "list")

# specifc range of combinations
combinations(4, 2, nitem = 2, skip = 3)

# specific combinations
combinations(4, 2, index = c(3, 5))

# random combinations
combinations(4, 2, nsample = 3)

# zero sized combinations
dim(combinations(5, 0))
dim(combinations(5, 6))
dim(combinations(0, 0))
dim(combinations(0, 1))

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