如何从所有组合中有效地随机选择多个组合? [英] How to randomly pick a number of combinations from all the combinations efficiently?
问题描述
我知道函数 combn
可以生成所有可能的组合。但是,如果成员总数很大,那确实是耗时和内存消耗。
I know function combn
can generate all the possible combinations. However, if the total number of members is large, this is really time-consuming and memory-consuming.
我的目标是从所有可能的组合中随机选择组合。例如,我想要3000个成员池中的5000个不同的三元组成员。我想我不需要生成所有可能的组合并从中选择5000个。但是R似乎没有现成的功能来执行此操作。那么如何解决这个问题呢?
My goal is to randomly pick combinations from all the possible combinations. For example, I want 5000 distinct triple set of members from a pool of 3000 members. I think I don't need to generate all possible combinations and choose 5000 from them. But seems that R doesn't have a ready-to-use function to do this. So how to deal with this problem?
推荐答案
这并不是您真正需要的,但是也许可以使您入门: p>
This is not exactly what you need but perhaps it can get you started:
library(data.table) #to make the table easier
members=1:3000;
X=data.table(RUN=1:5000)
X<-X[,as.list(sample(members, 3)),by=RUN]
这将创建3个从成员向量中随机选择的新列。看到它们作为每个成员的ID。
This will create 3 new columns that are randomly selected from the members vector. See them as IDs of each member.
我将使用以下方法进行检查,以查看有多少唯一身份:
I would do a check to see how many as unique using:
X[duplicated(X, by=c('V1','V2','V3'))]
这对您有帮助吗?
这篇关于如何从所有组合中有效地随机选择多个组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!