Learn R Programming

jwutil (version 1.2.3)

permuteWithRepeats: Generate all permutations of input, reusing values in each result row

Description

Expand the given vector into all possible values in each location, with or without duplicates.

Usage

permuteWithRepeats(x, unique = TRUE)

Arguments

x

list or vector

unique

logical, if TRUE, the default, only unique results are returned

Value

data frame, each row being one permutation

Examples

Run this code
# NOT RUN {
ltr <- c("a", "b", "c")
x <- permuteWithRepeats(ltr, unique = FALSE)
print(x)
stopifnot(nrow(x) == length(ltr)^length(ltr))
# duplicate results are dropped
y <- permuteWithRepeats(c("X", "Y", "Y"))
print(y)
stopifnot(nrow(y) == 2^3)
z <- permuteWithRepeats(c("X", "Y", "Y", "Y"))
stopifnot(nrow(z) == 2^4)
a <- permuteWithRepeats(c(1, 2, 3, 1))
stopifnot(nrow(a) == 3^4)
# }

Run the code above in your browser using DataLab