Lets say I have the following dataframe:
a <- c(0.9,0.8,0.7)
b <- c(1000,200,20)
c <- c(10,20,10)
myframe <- data.frame(a,b,c)
1 0.9 1000 10
2 0.8 200 20
3 0.7 20 10
I know want to find the "best" combination subject to the following rules:
I now order the dataframe like this...
myframe[with(myframe, order(-a, b, -c))[1],]
...and get the following return value:
a b c
1 0.9 1000 10
I now want to make the way "best" solution is chosen more "intelligent" by checking for the percentage deviation with the result that the second combination is chosen:
a b c
2 0.8 200 20
Change order of dataframe (already achieved, see above)
order(-a, b, -c)
(NEW) Check the b values of all lines where a is not more worse thant 15% of the best a value. Pick the line with the b value that is at least 50% better than the current b value.
now line 2 is chosen in case of the example above.
How can this be achieved?
Working with the data provided, it seems that you only need the subset
function. Is this what you are trying to achieve?
myframe[with(myframe, order(-a, b, -c))[1],]
best.a = myframe$a[1]
best.b = myframe$b[1]
subset(myframe,a > 0.85*best.a & b < 1.5*best.b)
a b c
1 0.9 1000 10
2 0.8 200 20