Search code examples
rvectorcombn

Create all combinations of items with many items


I want to have the possible combinations for a variety of items. Think of participants bringing one of three items to an event, and I want to know the different combinations (order of participants don't matter). For instance,

items <- rep(list(1:3), 5)
combinations <- expand.grid(items)
head(combinations)
  Var1 Var2 Var3 Var4 Var5
1    1    1    1    1    1
2    2    1    1    1    1
3    3    1    1    1    1
4    1    2    1    1    1
5    2    2    1    1    1
6    3    2    1    1    1

gives me the desired dataframe of combinations for 5 participants.

Now, imagine I have 50 participants. Then:

items <- rep(list(1:3), 50)
combinations <- expand.grid(items)
Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : 
  invalid 'times' value
In addition: Warning message:
In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
  NAs introduced by coercion to integer range

The problem is described here and arises due to limits on the size of vectors in R. So, it seems expand.grid is out of question.

Are there any alternatives to get my desired output in R? As far as I know, long vectors are supported in R since version 3.0, so I am a bit surprised to see that they are not yet implemented. Any pointers to alternatives are highly appreciated!


Solution

  • With 50 participants you create a dataframe with 3^50= 7.17898e+23 rows. Which is impossible to save in your memory. So I think it is a scaling problem.