Learn R Programming

arrangements (version 1.0.0)

Permutations: Permutations iterator

Description

This function returns a Permutations iterator which allows users to fetch the next permutation(s) via the getnext() method. All remaing permutations of the iterator can be fetched via the collect() method.

Usage

Permutations

ipermutations(n, k = n, x = NULL, freq = NULL, replace = FALSE)

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

Format

An object of class R6ClassGenerator of length 24.

Details

The Permutations class can be initialized by using the convenient wrapper ipermutations or

Permutations$new(n, k, x = NULL, freq = NULL, replace = FALSE)

getnext(d = 1L, type = NULL, drop = d == 1L && is.null(type))
collect(type = "r")
reset()
d

number of fetched arrangements

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

drop

vectorize a matrix or unlist a list

See Also

permutations for generating all permutations and npermutations to calculate number of permutations

Examples

Run this code
# NOT RUN {
iperm <- ipermutations(5, 2)
iperm$getnext()
iperm$getnext(2)
iperm$getnext(type = "c", drop = FALSE)
# collect remaining permutations
iperm$collect()

library(foreach)
foreach(x = ipermutations(5, 2), .combine=c) %do% {
  sum(x)
}
# }

Run the code above in your browser using DataLab