R:返回n个元素的所有可能排序的函数?

Fai*_*Ice 3 combinations r combinatorics

在R中,是否有一个函数返回n个元素的所有可能排序?我想要一个!通过n矩阵使得每行包含n个元素的所有可能的排序索引.也就是说,如果n = 3,我想:

 1,2,3 
 1,3,2,
 2,1,3,
 2,3,1,
 3,1,2,
 3,2,1
Run Code Online (Sandbox Code Playgroud)

我首先想到expand.grid做这个工作,并尝试:

n <- 3
expand.grid(rep(list(1:n),n))

   Var1 Var2 Var3
1     1    1    1
2     2    1    1
3     3    1    1
4     1    2    1
5     2    2    1
6     3    2    1
7     1    3    1
8     2    3    1
9     3    3    1
10    1    1    2
11    2    1    2
12    3    1    2
13    1    2    2
14    2    2    2
15    3    2    2
16    1    3    2
17    2    3    2
18    3    3    2
19    1    1    3
20    2    1    3
21    3    1    3
22    1    2    3
23    2    2    3
24    3    2    3
25    1    3    3
26    2    3    3
27    3    3    3
Run Code Online (Sandbox Code Playgroud)

但这会返回3 ^ 3乘3矩阵,每行可能包含重复值...

akr*_*run 7

尝试

library(gtools)
permutations(n,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
Run Code Online (Sandbox Code Playgroud)


小智 5

以下是一些选项:

n = 3

library(gtools)
permutations(n,n)

library(combinat)
permn(1:n)

library(multicool)
m = initMC(1:n)
allPerm(m)

library(iterpc)
I = iterpc(n, ordered = TRUE)
getall(I)
Run Code Online (Sandbox Code Playgroud)

我们来做一些基准测试

n=5
I = iterpc(n, ordered = TRUE)
m=initMC(1:n)

library(microbenchmark)
microbenchmark( permutations(n,n), permn(1:n), allPerm(m), getall(I) )

# Unit: microseconds
#               expr      min        lq    median        uq      max neval
# permutations(n, n) 2781.110 2857.9100 2922.7635 2973.5835 4877.656   100
#         permn(1:n) 3600.310 3664.0255 3722.6210 3772.4940 5475.748   100
#         allPerm(m)  325.026  458.1460  503.6575  558.8390  719.835   100
#          getall(I)  169.151  189.0615  245.5715  284.8245  472.937   100
Run Code Online (Sandbox Code Playgroud)

permutationsfrom gtoolspermnfrom cobinat都写在R中,而另外两个在C中,所以速度要快得多.
此外,multicool还提供了专门针对多集的排列设计的算法.所以iterpc对于简单的排列来说似乎是更好的选择:

f1 <- function(n){
m=initMC(1:n)
allPerm(m)}

f2 <- function(n){
I = iterpc(n, ordered = TRUE)
getall(I)}

microbenchmark(f1(10),f2(10))

# Unit: milliseconds
#   expr       min        lq    median        uq       max neval
# f1(10) 1143.8057 1216.4617 1322.2973 1335.5547 1383.8723   100
# f2(10)  421.7535  457.3466  461.4995  551.4025  648.8821   100
Run Code Online (Sandbox Code Playgroud)