lovetl2002
lovetl2002

Reputation: 1066

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

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?

Upvotes: 2

Views: 246

Answers (1)

Nikos
Nikos

Reputation: 3297

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?

Upvotes: 2

Related Questions