我正试图找到一种方法来有效地使用R中的矢量排列而不会炸毁我的计算机。这就是我想要做的事情:
n = 3 # I would need n>1000 instead, this is just to show what I am trying to achieve
t = 3
library(gtools)
m <- permutations(n = n, r = t, repeats.allowed = F, v = 1:n)
mm <- as.numeric(m)
df = data.frame()
for (i in 1:nrow(m)) {
mat <- matrix(0, nrow = ncol(m), ncol = n)
idx = m[i,]
mat[cbind(seq_along(idx), idx)] = 1
df = rbind(df, mat)
}
然而,使用排列,使用大n(例如,> 1000)对时间/内存消耗太大。它看起来像使用&#34; sample&#34;是一个很好的解决方案(建议here):
v = 1:n
N <- t(replicate(length(v)^4, sample(v, t)))
# compare with: permutations(n = n, r = t, repeats.allowed = F, v = 1:n)
sum(duplicated(N))
m <- N[!(duplicated(N)), ] # then continue with code above
但是,我仍然不确定要采取的样本数量,以确保涵盖所有可能性。有没有人对样品数量有任何想法,或者如何确保涵盖所有可能性?谢谢!