0

I'd like to have an algorithm (in r) that provides me with ALL possible combinations of integers that sum up to a certain sum. Preferably in a data.frame.

For instance:

subsets(4,3) should give me all combinations of 3 elements that sum up to 4:

 0,0,4
 0,4,0
 4,0,0
 1,0,3
 1,3,0
 3,0,1
 3,1,0
 0,1,3
 0,3,1
 1,1,2
 1,2,1
 2,1,1
 2,2,0
 0,2,2 
 2,0,2

My sum will be 50 at most and my groups are fixed to 6 (where 0 is allowed).

subsetsum() is not the answer to my problem unfortunately. Can anyone help me out?

dsent
  • 305
  • 1
  • 3
  • 9
  • 1
    what else have you tried so far? The `partitions` package will give you *all* (unordered) sets of non-negative integers that add up to a specified sum. From there you can subselect those with a maximum number of non-zero elements and compute the permutations you need. However, for 50/6, you're going to have a *lot* of elements; `pp <- part(50)` gives you the matrix of partitions, `sum(colSums(pp>0)<=6)` tells you that 9192 of them have <=6 non-zero components; multiply that by `factorial(6)` (all permutations) and you get 6618240 total sets. – Ben Bolker Jun 29 '17 at 00:00
  • hope this link will help https://stackoverflow.com/questions/22218640/getting-all-combinations-which-sum-up-to-100-using-r – BENY Jun 29 '17 at 01:48
  • A related question is [here](https://stackoverflow.com/questions/38081651/how-to-find-all-the-possible-k-integers-which-sum-of-them-equals-to-a-certain-nu/38082278). – lmo Jun 29 '17 at 12:33
  • Thank @Imo. Thanks exactly what I was looking for! – dsent Jul 03 '17 at 22:17

0 Answers0