How to randomly pick a number of combinations from all the combinations efficiently?

[亡魂溺海] 提交于 2019-12-24 05:16:11

问题


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.

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?


回答1:


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]

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'))]

Is this helping you at all?



来源:https://stackoverflow.com/questions/27125415/how-to-randomly-pick-a-number-of-combinations-from-all-the-combinations-efficien

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!