Learn R Programming

arrangements (version 1.0.0)

permutations: Permutations generator

Description

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

Usage

permutations(n, k = n, 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

ipermutations for iterating permutations and npermutations to calculate number of permutations

Examples

Run this code
# NOT RUN {
permutations(3)
permutations(x = LETTERS[1:3])

# choose 2 from 4
permutations(4, 2)
permutations(x = LETTERS[1:3], k = 2)

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

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

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

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

# zero sized partitions
dim(permutations(0))
dim(permutations(5, 0))
dim(permutations(5, 6))
dim(permutations(0, 0))
dim(permutations(0, 1))

# }

Run the code above in your browser using DataLab