从R中向量的所有可能排列中抽样

时间:2017-10-29 15:22:47

标签: r permutation iterpc

到目前为止,我发现使用iterpc生成排列是最快的方法。示例用法可以是:

library(iterpc)
set.seed(143)

dat <- sample(LETTERS[1:4], 10, replace = TRUE)
np_multiset(table(dat), length(dat))
# [1] 18900

I <- iterpc(table(dat), order=TRUE)
out <- getall(I)

getnext(I)
#   [1] A A A A B B C C D D
# Levels: A B C D

getcurrent(I)
#   [1] A A A A B B C C D D
# Levels: A B C D

得到的矩阵为18900×10,在单个矩阵中为large to be stored。在getnext(I, 1000)的帮助下,我可以获得1000个块的排列,并在此基础上工作。但是,所有这些排列都是带有标签的。有没有办法以随机顺序从序列中取样1000而不是顺序?

预期输出:(但是,没有生成所有排列out

Isam <- sample(18900, 10)
# [1] 15746 18026 17881 18687  7513  1975  5575  2845  1275 10207

out[Isam,]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#  [1,] "B"  "A"  "A"  "A"  "D"  "C"  "C"  "B"  "A"  "D"  
#  [2,] "B"  "D"  "A"  "A"  "C"  "D"  "A"  "C"  "B"  "A"  
#  [3,] "B"  "A"  "A"  "B"  "C"  "A"  "A"  "D"  "C"  "D"  
#  [4,] "A"  "C"  "A"  "C"  "D"  "B"  "A"  "B"  "A"  "D"  
#  [5,] "C"  "D"  "A"  "A"  "A"  "C"  "B"  "B"  "D"  "A"  
#  [6,] "A"  "B"  "A"  "D"  "A"  "D"  "A"  "B"  "C"  "C"  
#  [7,] "B"  "A"  "A"  "D"  "B"  "C"  "C"  "A"  "A"  "D"  
#  [8,] "A"  "A"  "D"  "C"  "B"  "D"  "A"  "A"  "C"  "B"  
#  [9,] "D"  "C"  "A"  "C"  "D"  "B"  "A"  "B"  "A"  "A"  
# [10,] "C"  "D"  "D"  "A"  "A"  "A"  "C"  "B"  "B"  "A" 

0 个答案:

没有答案