Learn R Programming

arrangements (version 1.0.0)

combinations: Combinations generator

Description

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

Usage

combinations(n, k, x = NULL, freq = NULL, replace = FALSE, type = "r")

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

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

See Also

icombinations for iterating combinations and ncombinations to calculate number of combinations

Examples

Run this code
# NOT RUN {
# choose 2 from 4
combinations(4, 2)
combinations(x = LETTERS[1:3], k = 2)

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

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

# column major
combinations(4, 2, type = "c")

# list output
combinations(4, 2, type = "l")

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

# }

Run the code above in your browser using DataLab