blazej blazej - 26 days ago 9
R Question

Print all permutations of a vector of characters in R

For starters, forgive my math terminology ignorance (I'll edit this question, once errors in my vocabulary are pointed out).

How can I print a complete set of 3 element permutation of a 10 element vector in R?

Let's assume a vector consists of 10 unique letters A to J

x<- LETTERS[seq( from = 1, to = 10 )]

I'd like to list (print) all possible 3 unique element permutations, for example:

ABC, ACB, ABD, ADB ... etc

Thank you for any hints

Answer Source

This can be easily done using the gtools package.

prm <- gtools::permutations(n=10, r=3, v=LETTERS[1:10])

Then you can apply paste0 across the rows to get a vector.

apply(prm, 1, function(x)paste0(x, collapse=''))