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?
You can use combn()
to get all possible combinations of 3 rows, then test each set of rows:
df <- data.frame(
x = c(50, 25, 35, 45, 12),
y = c(5, 6, 3, 7, 9)
)
rowsets <- combn(1:nrow(df), m = 3, simplify = FALSE)
x_sums <- vector("double", length = length(rowsets))
for (i in seq_along(rowsets)) {
df_rows <- df[rowsets[[i]], ]
x_sums[[i]] <- if (sum(df_rows$y) <= 20) sum(df_rows$x) else NA_real_
}
df[rowsets[[which.max(x_sums)]], ]
#> x y
#> 1 50 5
#> 3 35 3
#> 4 45 7
Created on 2022-03-01 by the reprex package (v2.0.1)