A.14 Using mutate and ifelse

Let’s add a column to the data set that indicates the location of the game. We had this explicitly before (we had a column home) but lost it when we rearranged our data. We’ll also create a column for the previous location.

Code
dd = dd %>%
  mutate(loc = ifelse(ha == 'home', 
                      team, 
                      opp), 
         prev.loc = lag(loc))

dd %>% 
  filter(team == 'PHI')
# A tibble: 82 × 10
# Groups:   team [1]
   date       team  score opp   opp.score gid     ha    days.rest loc   prev.loc
   <date>     <chr> <dbl> <chr>     <dbl> <chr>   <chr>     <dbl> <chr> <chr>   
 1 2021-10-20 PHI     117 NOP          97 221000… away         NA NOP   <NA>    
 2 2021-10-22 PHI     109 BKN         114 221000… home          2 PHI   NOP     
 3 2021-10-24 PHI     115 OKC         103 221000… away          2 OKC   PHI     
 4 2021-10-26 PHI      99 NYK         112 221000… away          2 NYK   OKC     
 5 2021-10-28 PHI     110 DET         102 221000… home          2 PHI   NYK     
 6 2021-10-30 PHI     122 ATL          94 221000… home          2 PHI   PHI     
 7 2021-11-01 PHI     113 POR         103 221000… home          2 PHI   PHI     
 8 2021-11-03 PHI     103 CHI          98 221001… home          2 PHI   PHI     
 9 2021-11-04 PHI     109 DET          98 221001… away          1 DET   PHI     
10 2021-11-06 PHI     114 CHI         105 221001… away          2 CHI   DET     
# ℹ 72 more rows