I would like to order my dataset according to the production levels in 2018-19. So company_code 3 goes first (5000), then company_code 1 (2000) and then company_code 2 (1000).
I have a dataset like this:
| company_code | financial_year | production |
|---|---|---|
| 1 | 2018-19 | 2000 |
| 1 | 2019-20 | 2500 |
| 1 | 2020-21 | 3000 |
| 1 | 2018-21 | 7500 |
| 2 | 2018-19 | 1000 |
| 2 | 2019-20 | 1500 |
| 2 | 2020-21 | 1000 |
| 2 | 2020-21 | 3500 |
| 3 | 2018-19 | 5000 |
| 3 | 2019-20 | 5500 |
| 3 | 2020-21 | 4000 |
| 3 | 2018-21 | 14500 |
I would like to end up with:
| company_code | financial_year | production |
|---|---|---|
| 3 | 2018-19 | 5000 |
| 3 | 2019-20 | 5500 |
| 3 | 2020-21 | 4000 |
| 3 | 2018-21 | 14500 |
| 1 | 2018-19 | 2000 |
| 1 | 2019-20 | 2500 |
| 1 | 2020-21 | 3000 |
| 1 | 2018-21 | 7500 |
| 2 | 2018-19 | 1000 |
| 2 | 2019-20 | 1500 |
| 2 | 2020-21 | 1000 |
| 2 | 2020-21 | 3500 |
I tried:
dataset <- dataset %>% mutate(COMPANY_CODE = reorder(COMPANY_CODE, -production[financial_year=="2018/19"]))
But this does not work, could anyone help? Many thanks