Reputation: 687
I'd like to know if there's a function or optimal way to generate all possible permutations when target vector is different for each element. Here's my example: First and third element from (a,b,c), and second one from (a,b,c,d). I'd like to have permutations of length 3 (36 of them):
aaa, aab, aac, aad
aba, aca, baa, caa
bab, bac, bad, cab
cac, cad, cba, cbb,
cbc, cbd, cca, ccb,
. . .
Thanks in advance,
Upvotes: 0
Views: 73
Reputation: 7312
You can do this with expand.grid()
like so:
v1 <- c("a","b","c")
v2 <- v1
v3 <- v1
vFull <- expand.grid(v1,v2,v3)
Upvotes: 0
Reputation: 5966
You can use merge with no by
parameter to make all possible combos.
v1 <- c("a","b","c")
v2 <- c("a","b","c", "d")
perm <- Reduce(function(...) merge(..., by =NULL), list(v1, v2, v1))
perm
# x y.x y.y
# 1 a a a
# 2 b a a
# 3 c a a
# 4 a b a
# 5 b b a
# 6 c b a
# 7 a c a
# 8 b c a
# 9 c c a
# 10 a d a
# 11 b d a
# 12 c d a
# 13 a a b
# 14 b a b
# 15 c a b
# 16 a b b
# 17 b b b
# 18 c b b
# 19 a c b
# 20 b c b
# 21 c c b
# 22 a d b
# 23 b d b
# 24 c d b
# 25 a a c
# 26 b a c
# 27 c a c
# 28 a b c
# 29 b b c
# 30 c b c
# 31 a c c
# 32 b c c
# 33 c c c
# 34 a d c
# 35 b d c
# 36 c d c
Upvotes: 1