Extract every combination of numeric values once they reach a target sum

I have a dataframe of two columns like so:

Name Salary
Name 1 5000000
Name 2 7000000
Name 3 9000000
Name 4 12000000
Name 5 14000000

I want to find every combination of salaries that reach 20,000,000. It can go over 20,000,000, but once it does, it stops. For example, names 1, 2 and 3 would be a valid combination, as would names 4 and 5.

I would also love to be able to view the salary combinations with their associates names in a dataframe.

Note that my dataframes typically have about 12 to 15 names across 30 different dataframes.

So far, I’ve tried to wrap my head around combn(), but I could only choose a specific number “m” as far as I know and could not choose a target number when the combinations stop. I also couldn’t get anywhere with the “sets” package.

  • With 15 names, that’s about 32,000 possible subsets, which isn’t really that many. I would just check them all. You could try to be a little smart about it starting with the smaller subsets and once you find one stop checking any other subsets include those elements. But I don’t think there’s going to be a much better way than just brute-forcing it… maybe with some linear programming package, but you don’t have an objective function so I’m not sure how well it would translate.

    – 

  • 3

    @jpsmith my understanding is that “valid” combinations should be minimal in that you can’t remove any elements without dipping below 20M. Said another way, if you have a set that exceeds 20M, you can’t add another name to the set and keep it valid. I think that’s what OP means with “It can go over 20,000,000, but once it does, it stops.” So since {1, 2, 3} is valid, any larger sets containing {1, 2, 3} are not valid.

    – 

Leave a Comment