0

I have a dataframe competition with columns branch, phone and sales

| branch   | phone   | sales|
|----------|---------|------|
| 123      | milky   | 654  |
| 456      | lemon   | 342  |
| 789      | blue    | 966  |
| 456      | blue    | 100  |
| 456      | milky   | 234  |
| 123      | lemon   | 874  |
| 789      | milky   | 234  |
| 123      | blue    | 332  |
| 789      | lemon   | 865  |

I want to show the highest number of sales for every phone: The output should be a dataframe winners that look like this

| branch   | phone   | sales|
|----------|---------|------|
| 123      | milky   | 654  |
| 789      | blue    | 966  |
| 123      | lemon   | 874  |

I tried order a dataframe by sales first, and then left only 3 top rows,

competition <- competition[order(competition$sales, decreasing = TRUE ),]
winners <- head(competition, 3)

But the output shows lemon phone two times with 874 and 865 sales

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

1
aggregrate(sales ~ phone, df, max)
Lennyy
  • 5,932
  • 2
  • 10
  • 23
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/9193372) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation. – Syscall Mar 12 '21 at 15:00