创建具有许多项目的项目的所有组合
Create all combinations of items with many items
我想要各种物品的可能组合。想想参与者将三件物品中的一件带到活动中,我想知道不同的组合(参与者的顺序无关紧要)。例如,
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
为我提供了 5 个参与者所需的组合数据框。
现在,假设我有 50 名参与者。那么:
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
问题已描述 here 并且由于 R 中向量大小的限制而出现。因此,似乎 expand.grid
是不可能的。
是否有其他方法可以在 R
中获得我想要的输出?据我所知,R
从 3.0 版本开始就支持长向量,所以我有点惊讶地看到它们还没有实现。非常感谢任何指向替代方案的指示!
如果有 50 名参与者,您将创建一个包含 3^50= 7.17898e+23 行的数据框。这是不可能保存在你的记忆中的。所以我认为这是一个缩放问题。
我想要各种物品的可能组合。想想参与者将三件物品中的一件带到活动中,我想知道不同的组合(参与者的顺序无关紧要)。例如,
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
为我提供了 5 个参与者所需的组合数据框。
现在,假设我有 50 名参与者。那么:
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
问题已描述 here 并且由于 R 中向量大小的限制而出现。因此,似乎 expand.grid
是不可能的。
是否有其他方法可以在 R
中获得我想要的输出?据我所知,R
从 3.0 版本开始就支持长向量,所以我有点惊讶地看到它们还没有实现。非常感谢任何指向替代方案的指示!
如果有 50 名参与者,您将创建一个包含 3^50= 7.17898e+23 行的数据框。这是不可能保存在你的记忆中的。所以我认为这是一个缩放问题。