Last but not least, you need to remove the grouping before you want to change the level of the computation.
# Ungroup the data
data % > %
filter(HR > 0) % > %
group_by(playerID) % > %
summarise(average_HR_game = sum(HR) / sum(G)) % > %
ungroup() % > %
summarise(total_average_homerun = mean(average_HR_game))
Code Explanation
- filter(HR >0) : Exclude zero homerun
- group_by(playerID): group by player
- summarise(average_HR_game = sum(HR)/sum(G)): Compute average homerun by player
- ungroup(): remove the grouping
- summarise(total_average_homerun = mean(average_HR_game)): Summarize the data