Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

compositions

Compositions generator


Description

This function generates the compositions of an non-negative interger n into k parts or parts of any sizes. The results are in lexicographical or reversed lexicographical order.

Usage

compositions(n, k = NULL, descending = FALSE, layout = NULL,
  nitem = -1L, skip = NULL, index = NULL, nsample = NULL,
  drop = NULL)

Arguments

n

an non-negative integer to be partitioned

k

number of parts

descending

an logical to use reversed lexicographical order

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 compositions required, usually used with skip

skip

the number of compositions skipped

index

a vector of indices of the desired compositions

nsample

sampling random compositions

drop

vectorize a matrix or unlist a list

See Also

icompositions for iterating compositions and ncompositions to calculate number of compositions

Examples

# all compositions of 4
compositions(4)
# reversed lexicographical order
compositions(4, descending = TRUE)

# fixed number of parts
compositions(6, 3)
# reversed lexicographical order
compositions(6, 3, descending = TRUE)

# column major
compositions(4, layout = "column")
compositions(6, 3, layout = "column")

# list output
compositions(4, layout = "list")
compositions(6, 3, layout = "list")

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

arrangements

Fast Generators and Iterators for Permutations, Combinations, Integer Partitions and Compositions

v1.1.9
MIT + file LICENSE
Authors
Randy Lai [aut, cre]
Initial release
2020-09-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.