Here is my dataframe:
df <- structure(list(Location = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("buildinga", "buildingb", "buildingc"), class = "factor"), Category = structure(c(1L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 1L), .Label = c("Food", "Beverage", "Food"),
class = "factor"),
units_sold = c(200, 250, 150, 180, 200, 80, 140, 200, 210)), class = "data.frame", row.names = c(NA,
-9L))
print(df)
Location Category units_sold
1 buildinga Food 200
2 buildinga Food 250
3 buildinga Beverage 150
4 buildingb Food 180
5 buildingb Beverage 200
6 buildingb Food 80
7 buildingc Beverage 140
8 buildingc Beverage 200
9 buildingc Food 210
I would like to pull the top two values from units sold for both Beverage and Food for each location. This will tell me what the top two grouped products are per location when looking at units sold.
I was trying this if-else statement but did not get any output:
ifelse(Location == Location & Category == Category & units_sold == max(units_sold), new_df
I tried using group_by with arrange order but I could not get the output I wanted. Goal is to export the 2 items per location as a csv file.