[英]Find the highest combination of values in column 1 based on max value in column 2 - R
I have a dataframe with 3 columns(in reality of ~2000 rows). 我有一个3列的数据框(实际上是2000行)。 I want to return the values of column A which has the highest maximum total score in column B, with the restriction of a max value in column C. For example: 我想返回A列的值,该列在B列中的最高总得分最高,但限制了C列中的最大值。例如:
colName col1 col2
John 7 50
Jim 3 25
James 2 25
Peter 9 110
Robert 1 75
Hank 1 75
Let's say the max total/sum value of col2 is 100. The highest combination of combined values in col1 would be 7+3+2=12. 假设col2的最大总和值为100。col1中组合值的最高组合为7 + 3 + 2 = 12。 I would like to return the list of names that would be returned: John, Jim, James 我想返回将要返回的姓名列表:John,Jim,James
Q1: How would I do this? Q1:我该怎么做?
Q2: Alternatively, It would be interesting to return the entire rows of the dataframe with the values of the highest possible combination in a dataframe: 问题2:或者,返回数据帧的整个行以及数据帧中最大可能组合的值会很有趣:
colName col1 col2
John 7 50
Jim 3 25
James 2 25
What you're referring to is a 0/1 knapsack problem . 您指的是一个0/1背包问题 。 The adagio
package implements a knapsack solver in R. adagio
软件包在R中实现了背包求解器。
> tbl
# A tibble: 6 x 3
colName col1 col2
<chr> <int> <int>
1 John 7 50
2 Jim 3 25
3 James 2 25
4 Peter 9 110
5 Robert 1 75
6 Hank 1 75
> tbl <- tbl[tbl$col2 <= 100, ] # `adagio` will complain otherwise
> soln <- knapsack(tbl$col2, tbl$col1, 100)
> tbl[soln$indices, ]
# A tibble: 3 x 3
colName col1 col2
<chr> <int> <int>
1 John 7 50
2 Jim 3 25
3 James 2 25
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.