Creating permutations and combinations with replacement
gtools
library(gtools)
## Combination with replacement
res.comb <- combinations(n = 2, r = 4, v = c("●","○"), repeats.allowed = TRUE)
print(res.comb, quote = FALSE)
[,1] [,2] [,3] [,4]
[1,] ○ ○ ○ ○
[2,] ○ ○ ○ ●
[3,] ○ ○ ● ●
[4,] ○ ● ● ●
[5,] ● ● ● ●
## Permutation with replacement
res.perm <- permutations(n = 2, r = 4, v = c("●","○"), repeats.allowed = TRUE)
print(res.perm, quote = FALSE)
[,1] [,2] [,3] [,4]
[1,] ○ ○ ○ ○
[2,] ○ ○ ○ ●
[3,] ○ ○ ● ○
[4,] ○ ○ ● ●
[5,] ○ ● ○ ○
[6,] ○ ● ○ ●
[7,] ○ ● ● ○
[8,] ○ ● ● ●
[9,] ● ○ ○ ○
[10,] ● ○ ○ ●
[11,] ● ○ ● ○
[12,] ● ○ ● ●
[13,] ● ● ○ ○
[14,] ● ● ○ ●
[15,] ● ● ● ○
[16,] ● ● ● ●
## Reformat
res.perm <- apply(res.perm, MARGIN = 1, paste, collapse = " ")
res.perm <- data.frame(perm = res.perm)
print(res.perm, row.names = FALSE)
perm
○ ○ ○ ○
○ ○ ○ ●
○ ○ ● ○
○ ○ ● ●
○ ● ○ ○
○ ● ○ ●
○ ● ● ○
○ ● ● ●
● ○ ○ ○
● ○ ○ ●
● ○ ● ○
● ○ ● ●
● ● ○ ○
● ● ○ ●
● ● ● ○
● ● ● ●
Create permutations of different sizes
size <- 5
list.perm <- lapply(seq_len(size), function(i) {
permutations(n = 2, r = i, v = c("●","○"), repeats.allowed = TRUE)
})
library(plyr)
l_ply(list.perm, print, quote = F)
[,1]
[1,] ○
[2,] ●
[,1] [,2]
[1,] ○ ○
[2,] ○ ●
[3,] ● ○
[4,] ● ●
[,1] [,2] [,3]
[1,] ○ ○ ○
[2,] ○ ○ ●
[3,] ○ ● ○
[4,] ○ ● ●
[5,] ● ○ ○
[6,] ● ○ ●
[7,] ● ● ○
[8,] ● ● ●
[,1] [,2] [,3] [,4]
[1,] ○ ○ ○ ○
[2,] ○ ○ ○ ●
[3,] ○ ○ ● ○
[4,] ○ ○ ● ●
[5,] ○ ● ○ ○
[6,] ○ ● ○ ●
[7,] ○ ● ● ○
[8,] ○ ● ● ●
[9,] ● ○ ○ ○
[10,] ● ○ ○ ●
[11,] ● ○ ● ○
[12,] ● ○ ● ●
[13,] ● ● ○ ○
[14,] ● ● ○ ●
[15,] ● ● ● ○
[16,] ● ● ● ●
[,1] [,2] [,3] [,4] [,5]
[1,] ○ ○ ○ ○ ○
[2,] ○ ○ ○ ○ ●
[3,] ○ ○ ○ ● ○
[4,] ○ ○ ○ ● ●
[5,] ○ ○ ● ○ ○
[6,] ○ ○ ● ○ ●
[7,] ○ ○ ● ● ○
[8,] ○ ○ ● ● ●
[9,] ○ ● ○ ○ ○
[10,] ○ ● ○ ○ ●
[11,] ○ ● ○ ● ○
[12,] ○ ● ○ ● ●
[13,] ○ ● ● ○ ○
[14,] ○ ● ● ○ ●
[15,] ○ ● ● ● ○
[16,] ○ ● ● ● ●
[17,] ● ○ ○ ○ ○
[18,] ● ○ ○ ○ ●
[19,] ● ○ ○ ● ○
[20,] ● ○ ○ ● ●
[21,] ● ○ ● ○ ○
[22,] ● ○ ● ○ ●
[23,] ● ○ ● ● ○
[24,] ● ○ ● ● ●
[25,] ● ● ○ ○ ○
[26,] ● ● ○ ○ ●
[27,] ● ● ○ ● ○
[28,] ● ● ○ ● ●
[29,] ● ● ● ○ ○
[30,] ● ● ● ○ ●
[31,] ● ● ● ● ○
[32,] ● ● ● ● ●