Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to generate n! permutations for a given n in R

Tags:

r

permutation

suppose n=3 then output should be: a vector containing vectors: 123 213 132 231 321

like image 221
kuber Avatar asked Dec 12 '22 21:12

kuber


2 Answers

This will solve your question:

install.packages("combinat")
require(combinat)
permn(1:3)

Also the functions: choose, combn ,expand.grid Might prove useful for you in the future.

like image 131
Tal Galili Avatar answered Dec 28 '22 08:12

Tal Galili


library(gtools)
permutations(3,3)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    3    2
[3,]    2    1    3
[4,]    2    3    1
[5,]    3    1    2
[6,]    3    2    1
like image 33
George Dontas Avatar answered Dec 28 '22 07:12

George Dontas