A.12 Using mutate
and group_by
When we add/edit a column with mutate
after group_by
has been used, the operation is performed on each group separately. For example, for each team and each game, let’s find how many days have passed since the team’s previous game.
Code
# A tibble: 82 × 8
# Groups: team [1]
date team score opp opp.score gid ha days.rest
<date> <chr> <dbl> <chr> <dbl> <chr> <chr> <dbl>
1 2021-10-20 PHI 117 NOP 97 22100009 away NA
2 2021-10-22 PHI 109 BKN 114 22100021 home 2
3 2021-10-24 PHI 115 OKC 103 22100038 away 2
4 2021-10-26 PHI 99 NYK 112 22100050 away 2
5 2021-10-28 PHI 110 DET 102 22100065 home 2
6 2021-10-30 PHI 122 ATL 94 22100082 home 2
7 2021-11-01 PHI 113 POR 103 22100096 home 2
8 2021-11-03 PHI 103 CHI 98 22100111 home 2
9 2021-11-04 PHI 109 DET 98 22100119 away 1
10 2021-11-06 PHI 114 CHI 105 22100135 away 2
# ℹ 72 more rows