0

I have a dataframe with three columns e.g.

a <- c(1,214,124,124,64,43)
b <- c(0.1,0.9,0.8,0.25,0.3,0.11)
c <- c(1000,12311,2221,5676,3234,7346)
myframe <- data.frame(a,b,c)

Now I want to find the best combination of a, b and c. The best combination would be where a is the highest, b the lowest and c the highest. If there are more than one combination where b and c are the lowest respective highest, a should be the deciding factor.

How can this be achived?

user3347232
  • 317
  • 1
  • 5
  • 14
  • 4
    Try reading `?order` – ilir Oct 15 '14 at 13:12
  • 2
    what is the desired result with the data you provided? – GSee Oct 15 '14 at 13:13
  • why you name a vector with "c"? Please never use "c" as a new vector name as its an inbuilt object in R , "c" is a generic function which combines its arguments, Read ?c – PKumar Oct 15 '14 at 13:26
  • I am sorry, but obviously I did not explain what I want correctly. There should always be a combination returned in the case of my example row 2 (214,0.9,12311) is the correct answer. – user3347232 Oct 15 '14 at 13:58

2 Answers2

3

Updated, based on your edit. If you want to order the rows of dataframe by highest a, lowest b, lowest c (applying the conditions in that order), then use:

with(myframe, order(-a, b, c))

Output (row order):

[1] 2 4 3 5 6 1

If you want just the 'best' single row (i.e. the first result from the ordering above):

myframe[with(myframe, order(-a, b, c))[1],]

Output:

    a    b     c
2 214 0.90 12311
arvi1000
  • 8,197
  • 1
  • 29
  • 49
0

To find "the best combination where a is the highest, b the lowest and c the highest":

> apply(myframe, 1, function(x) ifelse(x[1]==max(myframe$a) && x[2]==min(myframe$b) && x[3]==max(myframe$c), x,""  ))
[1] "" "" "" "" "" ""

There seems to be no such combination in current data frame.

rnso
  • 20,794
  • 19
  • 81
  • 167