A.7 Ordering rows with arrange

If we want to find the game with the smallest diff (the most negative diff), we could order the rows by diff, using order in base R.

Code
head(d[order(d$diff),])
           date      gid away home ascore hscore diff
865  2022-02-15 22100869  BOS  PHI    135     87  -48
627  2022-01-14 22100637  GSW  CHI    138     96  -42
1029 2022-03-15 22101030  BKN  ORL    150    108  -42
885  2022-02-24 22100894  GSW  POR    132     95  -37
951  2022-03-04 22100951  MIN  OKC    138    101  -37
1084 2022-03-23 22101094  SAS  POR    133     96  -37

Instead we could use arrange from dplyr like this:

Code
d %>% 
  arrange(diff) %>%
  head()
        date      gid away home ascore hscore diff
1 2022-02-15 22100869  BOS  PHI    135     87  -48
2 2022-01-14 22100637  GSW  CHI    138     96  -42
3 2022-03-15 22101030  BKN  ORL    150    108  -42
4 2022-02-24 22100894  GSW  POR    132     95  -37
5 2022-03-04 22100951  MIN  OKC    138    101  -37
6 2022-03-23 22101094  SAS  POR    133     96  -37

To order for largest to smallest, use desc().

Code
d %>% 
  arrange(desc(diff)) %>%
  head()
        date      gid away home ascore hscore diff
1 2021-12-02 22100330  OKC  MEM     79    152   73
2 2022-01-25 22100717  SAC  BOS     75    128   53
3 2022-04-08 22101209  POR  DAL     78    128   50
4 2022-04-10 22101222  OKC  LAC     88    138   50
5 2022-01-11 22100415  DET  CHI     87    133   46
6 2021-12-26 22100494  TOR  CLE     99    144   45

To break ties, we could include another column in arrange. Let’s break ties by using hscore, so that if diff is the same, the row with the higher hscore comes first.

Code
d %>% 
  arrange(desc(diff), 
          desc(hscore)) %>%
  head()
        date      gid away home ascore hscore diff
1 2021-12-02 22100330  OKC  MEM     79    152   73
2 2022-01-25 22100717  SAC  BOS     75    128   53
3 2022-04-10 22101222  OKC  LAC     88    138   50
4 2022-04-08 22101209  POR  DAL     78    128   50
5 2022-01-11 22100415  DET  CHI     87    133   46
6 2021-12-26 22100494  TOR  CLE     99    144   45

Note that we put these two column names to sort by on differnt lines.