【发布时间】:2017-10-16 19:53:49
【问题描述】:
我有三个变量:X、Y 和 Z。我想找到 X、Y 和 Z 的所有组合加起来为 100。X、Y 和 Z 只能取 [0,100] 之间的值。输出应该看起来像这样:
X Y Z Sum
100 0 0 100
99 1 0 100
99 0 1 100
98 2 0 100
98 1 1 100
98 0 2 100
等等……
关于如何获得所有可能的组合的任何建议?
【问题讨论】:
-
建议的骗子:r - Unique Combinations of all elements from two or more vectors、How to generate matrix of combinations?。生成所有组合,使用
rowSums,以及行和== 100的子集。 -
我能找到的最好方法是将一个数字固定为常数,问题会容易得多。例如,当 x = 0 时,有 101 种 y 和 z 组合。当 x = 1 时,y 和 z 有 100 种组合。以此类推。
-
partitions::composition(100, 3); Generating all permutations of N balls in M bins -
Sum列真的有必要吗?
标签: r combinations