2

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?

lovetl2002
  • 910
  • 9
  • 23

1 Answers1

2

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?

Nikos
  • 3,267
  • 1
  • 25
  • 32
  • It is helpful. But seems that I need a while loop to make sure that all sets are unique. – lovetl2002 Nov 25 '14 at 16:07
  • With the last entry (X[duplicated(X, by=c('V1','V2','V3'))]) you can check if there are duplicates and remove them. In any case, run it for 10,000 and get a random sample of 5000. Unless you are very very unlucky :) – Nikos Nov 25 '14 at 16:14