Import your data

data <- read_csv("../00_data/MyData.csv")
## New names:
## Rows: 380 Columns: 23
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (6): Date, HomeTeam, AwayTeam, FTR, HTR, Referee dbl (17): ...1, FTHG, FTAG,
## HTHG, HTAG, HS, AS, HST, AST, HF, AF, HC, AC, HY...
## ℹ 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.
## • `` -> `...1`

Chapter 14

Tools

Detect matches

data$HomeTeam
##   [1] "Brentford"      "Man United"     "Burnley"        "Chelsea"       
##   [5] "Everton"        "Leicester"      "Watford"        "Norwich"       
##   [9] "Newcastle"      "Tottenham"      "Liverpool"      "Aston Villa"   
##  [13] "Crystal Palace" "Leeds"          "Man City"       "Brighton"      
##  [17] "Southampton"    "Wolves"         "Arsenal"        "West Ham"      
##  [21] "Man City"       "Aston Villa"    "Brighton"       "Newcastle"     
##  [25] "Norwich"        "West Ham"       "Liverpool"      "Burnley"       
##  [29] "Tottenham"      "Wolves"         "Crystal Palace" "Arsenal"       
##  [33] "Brentford"      "Leicester"      "Man United"     "Southampton"   
##  [37] "Watford"        "Chelsea"        "Leeds"          "Everton"       
##  [41] "Newcastle"      "Wolves"         "Burnley"        "Liverpool"     
##  [45] "Man City"       "Norwich"        "Aston Villa"    "Brighton"      
##  [49] "West Ham"       "Tottenham"      "Chelsea"        "Man United"    
##  [53] "Everton"        "Leeds"          "Leicester"      "Watford"       
##  [57] "Brentford"      "Southampton"    "Arsenal"        "Crystal Palace"
##  [61] "Man United"     "Burnley"        "Chelsea"        "Leeds"         
##  [65] "Wolves"         "Brighton"       "Crystal Palace" "Tottenham"     
##  [69] "West Ham"       "Liverpool"      "Watford"        "Aston Villa"   
##  [73] "Leicester"      "Man City"       "Norwich"        "Southampton"   
##  [77] "Brentford"      "Everton"        "Newcastle"      "Arsenal"       
##  [81] "Arsenal"        "Chelsea"        "Crystal Palace" "Everton"       
##  [85] "Leeds"          "Southampton"    "Brighton"       "Brentford"     
##  [89] "West Ham"       "Man United"     "Leicester"      "Burnley"       
##  [93] "Liverpool"      "Man City"       "Newcastle"      "Watford"       
##  [97] "Tottenham"      "Norwich"        "Aston Villa"    "Wolves"        
## [101] "Southampton"    "Man United"     "Brentford"      "Chelsea"       
## [105] "Crystal Palace" "Brighton"       "Arsenal"        "Everton"       
## [109] "Leeds"          "West Ham"       "Leicester"      "Aston Villa"   
## [113] "Burnley"        "Newcastle"      "Norwich"        "Watford"       
## [117] "Wolves"         "Liverpool"      "Man City"       "Tottenham"     
## [121] "Arsenal"        "Crystal Palace" "Liverpool"      "Norwich"       
## [125] "Brighton"       "Brentford"      "Leicester"      "Man City"      
## [129] "Chelsea"        "Newcastle"      "Leeds"          "Southampton"   
## [133] "Watford"        "West Ham"       "Wolves"         "Aston Villa"   
## [137] "Everton"        "Tottenham"      "Man United"     "West Ham"      
## [141] "Newcastle"      "Southampton"    "Wolves"         "Watford"       
## [145] "Leeds"          "Man United"     "Tottenham"      "Aston Villa"   
## [149] "Everton"        "Brentford"      "Man City"       "Arsenal"       
## [153] "Chelsea"        "Liverpool"      "Norwich"        "Burnley"       
## [157] "Leicester"      "Crystal Palace" "Norwich"        "Man City"      
## [161] "Brighton"       "Crystal Palace" "Arsenal"        "Chelsea"       
## [165] "Liverpool"      "Leeds"          "Newcastle"      "Wolves"        
## [169] "Tottenham"      "Man City"       "Norwich"        "Tottenham"     
## [173] "West Ham"       "Aston Villa"    "Brighton"       "Newcastle"     
## [177] "Crystal Palace" "Southampton"    "Watford"        "Leicester"     
## [181] "Chelsea"        "Brentford"      "Man United"     "Arsenal"       
## [185] "Watford"        "Crystal Palace" "Brentford"      "Everton"       
## [189] "Leeds"          "Chelsea"        "Man United"     "Southampton"   
## [193] "West Ham"       "Brighton"       "Man City"       "Newcastle"     
## [197] "Norwich"        "Wolves"         "Aston Villa"    "Liverpool"     
## [201] "West Ham"       "Brighton"       "Leicester"      "Brentford"     
## [205] "Watford"        "Everton"        "Brentford"      "Leeds"         
## [209] "Man United"     "Southampton"    "Arsenal"        "Crystal Palace"
## [213] "Leicester"      "Chelsea"        "Burnley"        "Newcastle"     
## [217] "West Ham"       "Burnley"        "Man City"       "Norwich"       
## [221] "Tottenham"      "Aston Villa"    "Liverpool"      "Wolves"        
## [225] "Man United"     "Brentford"      "Everton"        "Watford"       
## [229] "Norwich"        "Burnley"        "Newcastle"      "Tottenham"     
## [233] "Leicester"      "Man United"     "West Ham"       "Arsenal"       
## [237] "Aston Villa"    "Brighton"       "Crystal Palace" "Liverpool"     
## [241] "Southampton"    "Man City"       "Leeds"          "Wolves"        
## [245] "Burnley"        "Watford"        "Liverpool"      "Arsenal"       
## [249] "Southampton"    "Leeds"          "Brentford"      "Crystal Palace"
## [253] "Man United"     "Brighton"       "Everton"        "West Ham"      
## [257] "Burnley"        "Leicester"      "Aston Villa"    "Burnley"       
## [261] "Newcastle"      "Norwich"        "Wolves"         "Liverpool"     
## [265] "Watford"        "Man City"       "Tottenham"      "Norwich"       
## [269] "Southampton"    "Wolves"         "Leeds"          "Brighton"      
## [273] "Brentford"      "Man United"     "Chelsea"        "Everton"       
## [277] "Leeds"          "Southampton"    "West Ham"       "Arsenal"       
## [281] "Crystal Palace" "Brighton"       "Arsenal"        "Everton"       
## [285] "Wolves"         "Aston Villa"    "Leicester"      "Tottenham"     
## [289] "Liverpool"      "Brighton"       "Burnley"        "Chelsea"       
## [293] "Leeds"          "Wolves"         "Man United"     "West Ham"      
## [297] "Tottenham"      "Crystal Palace" "Burnley"        "Newcastle"     
## [301] "Everton"        "Arsenal"        "Southampton"    "Watford"       
## [305] "Aston Villa"    "Brentford"      "Leicester"      "Norwich"       
## [309] "Man City"       "Tottenham"      "Man United"     "Southampton"   
## [313] "Watford"        "Newcastle"      "West Ham"       "Liverpool"     
## [317] "Chelsea"        "Everton"        "Newcastle"      "Man City"      
## [321] "Burnley"        "Arsenal"        "Leicester"      "Man City"      
## [325] "Norwich"        "Brentford"      "Brighton"       "Burnley"       
## [329] "Chelsea"        "Liverpool"      "Crystal Palace" "Man United"    
## [333] "Newcastle"      "Aston Villa"    "Southampton"    "Watford"       
## [337] "Wolves"         "Leeds"          "Everton"        "Tottenham"     
## [341] "West Ham"       "Man United"     "Brentford"      "Burnley"       
## [345] "Chelsea"        "Crystal Palace" "Brighton"       "Liverpool"     
## [349] "Arsenal"        "Leicester"      "Norwich"        "Man City"      
## [353] "Aston Villa"    "Leeds"          "Leicester"      "Watford"       
## [357] "Wolves"         "Tottenham"      "Tottenham"      "Aston Villa"   
## [361] "Leeds"          "Watford"        "West Ham"       "Wolves"        
## [365] "Everton"        "Newcastle"      "Southampton"    "Everton"       
## [369] "Aston Villa"    "Chelsea"        "Arsenal"        "Brentford"     
## [373] "Brighton"       "Burnley"        "Chelsea"        "Crystal Palace"
## [377] "Leicester"      "Liverpool"      "Man City"       "Norwich"
str_detect(data$HomeTeam, "Man")
##   [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
## [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [145] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [157] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [169] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
## [193] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [205] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [217] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
## [229] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [241] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [253]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [265] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
## [277] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [289] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [301] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
## [313] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
## [325] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
## [337] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [349] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [361] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [373] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
sum(str_detect(data$HomeTeam, "Man"))
## [1] 38
data %>%
    summarise(num_Man = sum(str_detect(HomeTeam, "Man")))
## # A tibble: 1 × 1
##   num_Man
##     <int>
## 1      38

Extract matches

data %>%
    mutate(col_Man = str_extract(HomeTeam, "Man")) %>%
    select(HomeTeam, col_Man) %>%
    filter(!is.na(col_Man))
## # A tibble: 38 × 2
##    HomeTeam   col_Man
##    <chr>      <chr>  
##  1 Man United Man    
##  2 Man City   Man    
##  3 Man City   Man    
##  4 Man United Man    
##  5 Man City   Man    
##  6 Man United Man    
##  7 Man United Man    
##  8 Man City   Man    
##  9 Man United Man    
## 10 Man City   Man    
## # ℹ 28 more rows

Replacing matches

data %>%
    mutate(col_ford = str_replace(HomeTeam, "Man", "ford")) %>%
    select(HomeTeam, col_ford)
## # A tibble: 380 × 2
##    HomeTeam   col_ford   
##    <chr>      <chr>      
##  1 Brentford  Brentford  
##  2 Man United ford United
##  3 Burnley    Burnley    
##  4 Chelsea    Chelsea    
##  5 Everton    Everton    
##  6 Leicester  Leicester  
##  7 Watford    Watford    
##  8 Norwich    Norwich    
##  9 Newcastle  Newcastle  
## 10 Tottenham  Tottenham  
## # ℹ 370 more rows