Test assignment: Highest amount of free wine given by day/driver combination?
note: need perform this work with tidyverse library only - can't load any other library
Need help with my code:
pizza %>%
select(day,driver,free_wine)%>%
group_by(day,driver)%>%
summarise(n=sum(free_wine,na.rm = TRUE),.groups = 'drop')
My output is not correct (showing multiple line items for each day). I understand that I need a code line to show max value by day and driver here but unable to figure out how to do it without impacting groupby configuration
Expected out should be one row for each day showing max value -
Example
Day Driver n Friday Sam 20 Thursday Tom 12 Wenesday Rick 15
CodePudding user response:
Try the following. After adding the free_wine
amounts per driver and day, filter
the maximal values.
pizza %>%
group_by(day, driver) %>%
summarise(free_wine = sum(free_wine, na.rm = TRUE), .groups = 'keep') %>%
filter(free_wine == max(free_wine))
An alternative is to drop the groups an group again but this time by year only.
pizza %>%
group_by(day, driver)%>%
summarise(free_wine = sum(free_wine,na.rm = TRUE),.groups = 'drop') %>%
group_by(day) %>%
filter(free_wine == max(free_wine))