如何用R计算组标签的排列?

alb*_*rto 2 r permutation permute

给出一个矢量:

labels <- c(1,2,3,3,3)
Run Code Online (Sandbox Code Playgroud)

如何获得所有可能的群组重新标记?对于这个例子:

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

我一直在看permute包,但我不知道如何将它应用于这种情况.

Kha*_*haa 5

这个解决方案怎么样?

labels <- c(1,2,3,3,3)
library(data.table)
a <- do.call(cbind, combinat::permn(unique(labels)))
data.table(a)[,lapply(.SD, function(x)x[labels]),]
#   V1 V2 V3 V4 V5 V6
#1:  1  1  3  3  2  2
#2:  2  3  1  2  3  1
#3:  3  2  2  1  1  3
#4:  3  2  2  1  1  3
#5:  3  2  2  1  1  3
Run Code Online (Sandbox Code Playgroud)

要不就

apply(a, 2, function(x) x[labels])
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    1    3    3    2    2
#[2,]    2    3    1    2    3    1
#[3,]    3    2    2    1    1    3
#[4,]    3    2    2    1    1    3
#[5,]    3    2    2    1    1    3
Run Code Online (Sandbox Code Playgroud)