Import your data

games <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-02-04/games.csv')
## Rows: 5324 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (11): week, home_team, away_team, winner, tie, day, date, home_team_nam...
## dbl   (7): year, pts_win, pts_loss, yds_win, turnovers_win, yds_loss, turnov...
## time  (1): time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Chapter 14

Tools

Detect matches

games_small <- games %>% select(home_team, away_team, winner) %>% head(n=10)
games_small
## # A tibble: 10 × 3
##    home_team            away_team            winner              
##    <chr>                <chr>                <chr>               
##  1 Minnesota Vikings    Chicago Bears        Minnesota Vikings   
##  2 Kansas City Chiefs   Indianapolis Colts   Indianapolis Colts  
##  3 Washington Redskins  Carolina Panthers    Washington Redskins 
##  4 Atlanta Falcons      San Francisco 49ers  Atlanta Falcons     
##  5 Pittsburgh Steelers  Baltimore Ravens     Baltimore Ravens    
##  6 Cleveland Browns     Jacksonville Jaguars Jacksonville Jaguars
##  7 New England Patriots Tampa Bay Buccaneers Tampa Bay Buccaneers
##  8 New Orleans Saints   Detroit Lions        Detroit Lions       
##  9 New York Giants      Arizona Cardinals    New York Giants     
## 10 Dallas Cowboys       Philadelphia Eagles  Philadelphia Eagles
games_small %>%
    summarise(sum(str_detect(home_team, "^New")))
## # A tibble: 1 × 1
##   `sum(str_detect(home_team, "^New"))`
##                                  <int>
## 1                                    3

Extract matches

birds <- c("Arizona Cardinals", "Philadelphia Eagles", "Baltimore Ravens", "Seatle Seahawks", "Atlanta Falcons")
bird_teams <- str_c(birds, collapse = "|")
bird_teams
## [1] "Arizona Cardinals|Philadelphia Eagles|Baltimore Ravens|Seatle Seahawks|Atlanta Falcons"
# extract bird team games
bird_game <- str_subset(games_small$home_team, bird_teams)
str_extract(bird_game, bird_teams)
## [1] "Atlanta Falcons"
bird_game <- str_subset(games_small$away_team, bird_teams)
str_extract(bird_game, bird_teams)
## [1] "Baltimore Ravens"    "Arizona Cardinals"   "Philadelphia Eagles"

Replacing matches

games_small_rev <- games_small %>% mutate(winner_rev = winner %>% str_replace("Redskins", "Commanders"))

games_small_rev %>% select(winner_rev)
## # A tibble: 10 × 1
##    winner_rev           
##    <chr>                
##  1 Minnesota Vikings    
##  2 Indianapolis Colts   
##  3 Washington Commanders
##  4 Atlanta Falcons      
##  5 Baltimore Ravens     
##  6 Jacksonville Jaguars 
##  7 Tampa Bay Buccaneers 
##  8 Detroit Lions        
##  9 New York Giants      
## 10 Philadelphia Eagles