I came up with a more optimal semi-solution. I have sorted my dataframe by Sector and Volume.
df <- structure(list(Customer = structure(1:17, .Label = c("A", "B",
"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q"), class = "factor"), Sector = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("Aviation",
"Biotech", "Construction"), class = "factor"), Volume = c(-5000L,
-3000L, 4000L, 6000L, 7000L, 9000L, -4000L, -1500L, 2000L, 3000L,
5000L, 6000L, -7000L, -4000L, 5000L, 7000L, 8000L)),
class = "data.frame", row.names = c(NA,-17L))
EDITED:
## > df
## Customer Sector Volume
## A Aviation - 5000
## B Aviation - 3000
## C Aviation 4000
## D Aviation 6000
## E Aviation 7000
## F Aviation 9000
## G Biotech - 4000
## H Biotech - 1500
## I Biotech 2000
## J Biotech 3000
## K Biotech 5000
## L Biotech 6000
## M Construction - 7000
## N Construction - 4000
## O Construction 5000
## P Construction 7000
## Q Construction 8000
Let's say I would like to leave the highest and lowest 2 customers per sector. So, my final table should look like this:
## > df
## Customer Sector Volume
## A Aviation - 5000
## B Aviation - 3000
## E Aviation 7000
## F Aviation 9000
## G Biotech - 4000
## H Biotech - 1500
## K Biotech 5000
## L Biotech 6000
## M Construction - 7000
## N Construction - 4000
## P Construction 7000
## Q Construction 8000
The only difference is that I would like to see highest/lowest 100 customers per sector in my case instead of just 2.