Let’s say I have a dataframe df
like this:
Home Away HomeGoals AwayGoals HomePoints AwayPoints
1 Arsenal Chelsea 3 1 3 0
2 Chelsea Liverpool 0 0 1 1
3 Liverpool Arsenal 2 1 3 0
4 Chelsea Arsenal 4 5 0 3
5 Liverpool Chelsea 3 2 3 0
6 Arsenal Liverpool 2 2 1 1
I would like to produce a new dataframe FinalStandings
showing team standings, sorted by Total Points, Goal Difference and Goals Scored, in descending order. So I would need to introduce new columns Team
, TotalPoints
, GoalDiff
, and GoalsScored.
So for Arsenal, the corresponding row of FinalStandings
should look like this:
Team TotalPoints GoalDiff GoalsScored
Arsenal 7 2 11
I suppose I need to use the library(dplyr)
(or maybe some other way) along with some sort of aggregating group_by()
type functions, but I am struggling to implement it and get it to work. Please show how can one achieve the desired result efficiently?