tresbot tresbot - 7 months ago 29
R Question

Generating all distinct permutations of a list in R

I'm trying to create a list of permutations of a list, such that, for example,

perms(list("a", "b", "c"))

list(list("a", "b", "c"), list("a", "c", "b"), list("b", "a", "c"),
list("b", "c", "a"), list("c", "a", "b"), list("c", "b", "a"))

I'm not sure how to proceed, any help would be greatly appreciated.

Answer Source

combinat::permn will do that work:

> library(combinat)
> permn(letters[1:3])
[1] "a" "b" "c"

[1] "a" "c" "b"

[1] "c" "a" "b"

[1] "c" "b" "a"

[1] "b" "c" "a"

[1] "b" "a" "c"

Note that calculation is huge if the element is large.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download