'How do I choose the max sum of one column while the sum of another column is less than constant in R?

Say I have a data frame with 2 columns x and y.

How do I choose 3 elements from x so that it's sum is the largest possible while the sum of y stays under a certain amount.

    x  y
1  50  5
2  25  6
3  35  3
4  45  7
5  12  9

e.g. I want the sum of y to be <= 20 and the sum of x to be as large as possible. Then the largest sum of 3 elements from x would be entries 1,3,4 (50+35+45). What function can I use to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source