Search code examples
rdataframepercentagedeviation

Order dataframe according to specific deviation percentage (edited)


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:

  • for a higher is better, for b lower is better, for c higher is better
  • a trumps b trumps c

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
  1. Change order of dataframe (already achieved, see above)

    order(-a, b, -c)

  2. (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.

  3. now line 2 is chosen in case of the example above.

How can this be achieved?


Solution

  • Working with the data provided, it seems that you only need the subsetfunction. 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