Import your data

top_250 <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-03/top_250.csv')
## Rows: 251 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): player, years, raw_link, link, active
## dbl (4): raw_rank, total_goals, url_number, yr_start
## 
## ℹ 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

top_250$player
##   [1] "Wayne Gretzky"      "Gordie Howe"        "Jaromir Jagr"      
##   [4] "Brett Hull"         "Marcel Dionne"      "Phil Esposito"     
##   [7] "Mike Gartner"       "Alex Ovechkin"      "Mark Messier"      
##  [10] "Steve Yzerman"      "Mario Lemieux"      "Teemu Selanne"     
##  [13] "Luc Robitaille"     "Brendan Shanahan"   "Dave Andreychuk"   
##  [16] "Jarome Iginla"      "Joe Sakic"          "Bobby Hull"        
##  [19] "Dino Ciccarelli"    "Jari Kurri"         "Mark Recchi"       
##  [22] "Mike Bossy"         "Mats Sundin"        "Joe Nieuwendyk"    
##  [25] "Mike Modano"        "Patrick Marleau"    "Guy Lafleur"       
##  [28] "John Bucyk"         "Ron Francis"        "Michel Goulet"     
##  [31] "Maurice Richard"    "Stan Mikita"        "Keith Tkachuk"     
##  [34] "Frank Mahovlich"    "Marian Hossa"       "Bryan Trottier"    
##  [37] "Pat Verbeek"        "Dale Hawerchuk"     "Pierre Turgeon"    
##  [40] "Jeremy Roenick"     "Gilbert Perreault"  "Jean Beliveau"     
##  [43] "Peter Bondra"       "Joe Mullen"         "Lanny McDonald"    
##  [46] "Glenn Anderson"     "Jean Ratelle"       "Norm Ullman"       
##  [49] "Brian Bellows"      "Darryl Sittler"     "Sergei Fedorov"    
##  [52] "Bernie Nicholls"    "Denis Savard"       "Alexander Mogilny" 
##  [55] "Pat LaFontaine"     "Sidney Crosby"      "Alex Delvecchio"   
##  [58] "Theoren Fleury"     "Rod Brind'Amour"    "Peter Stastny"     
##  [61] "Doug Gilmour"       "Rick Middleton"     "Daniel Alfredsson" 
##  [64] "Ilya Kovalchuk"     "Steve Larmer"       "Rick Vaive"        
##  [67] "Rick Tocchet"       "Gary Roberts"       "Pavel Bure"        
##  [70] "Rick Nash"          "Eric Staal"         "Vincent Damphousse"
##  [73] "Dave Taylor"        "Alex Kovalev"       "Bill Guerin"       
##  [76] "Yvan Cournoyer"     "Brian Propp"        "Steve Shutt"       
##  [79] "Steven Stamkos"     "Owen Nolan"         "Vincent Lecavalier"
##  [82] "Stephane Richer"    "Steve Thomas"       "Bill Barber"       
##  [85] "Jason Arnott"       "Joe Thornton"       "Tony Amonte"       
##  [88] "John MacLean"       "Garry Unger"        "Evgeni Malkin"     
##  [91] "Ray Bourque"        "Patrik Elias"       "Ray Ferraro"       
##  [94] "Marian Gaborik"     "John LeClair"       "Rod Gilbert"       
##  [97] "Shane Doan"         "Paul Kariya"        "John Ogrodnick"    
## [100] "Paul Coffey"        "Dave Keon"          "Markus Naslund"    
## [103] "Pierre Larouche"    "Cam Neely"          "Tomas Sandstrom"   
## [106] "Bernie Geoffrion"   "Daniel Sedin"       "Martin St. Louis"  
## [109] "Dean Prentice"      "Jean Pronovost"     "Ryan Smyth"        
## [112] "Ray Whitney"        "Rick Martin"        "Patrick Kane"      
## [115] "Jeff Carter"        "Zach Parise"        "Reggie Leach"      
## [118] "Ted Lindsay"        "Claude Lemieux"     "Corey Perry"       
## [121] "Milan Hejduk"       "Butch Goring"       "Trevor Linden"     
## [124] "Thomas Vanek"       "Dany Heatley"       "Eric Lindros"      
## [127] "Rick Kehoe"         "Phil Kessel"        "Tim Kerr"          
## [130] "Bernie Federko"     "Joe Pavelski"       "Geoff Courtnall"   
## [133] "Jacques Lemaire"    "Scott Mellanby"     "Miroslav Satan"    
## [136] "Brent Sutter"       "Peter McNab"        "Ivan Boldirev"     
## [139] "Bobby Clarke"       "Henri Richard"      "Kirk Muller"       
## [142] "Bobby Smith"        "Ray Sheppard"       "Dennis Maruk"      
## [145] "Wilf Paiement"      "Vyacheslav Kozlov"  "Mike Foligno"      
## [148] "Kevin Dineen"       "Greg Adams"         "Geoff Sanderson"   
## [151] "Danny Gare"         "Patrice Bergeron"   "Andy Bathgate"     
## [154] "Rick MacLeish"      "John Tavares"       "Jonathan Toews"    
## [157] "Charlie Simmer"     "Scott Young"        "Brian Rolston"     
## [160] "Ed Olczyk"          "Adam Oates"         "Jason Spezza"      
## [163] "Dave Christian"     "Al MacInnis"        "Phil Housley"      
## [166] "Alexei Yashin"      "Glen Murray"        "Henrik Zetterberg" 
## [169] "Ron Ellis"          "Anze Kopitar"       "Wendel Clark"      
## [172] "Ziggy Palffy"       "Mike Bullard"       "Adam Graves"       
## [175] "Kevin Stevens"      "Ken Hodge"          "Scott Hartnell"    
## [178] "Bobby Holik"        "John Tonelli"       "Paul MacLean"      
## [181] "Nels Stewart"       "Pit Martin"         "Vic Hadfield"      
## [184] "Dale Hunter"        "Petr Sykora"        "Olli Jokinen"      
## [187] "Tony McKegney"      "Bob Carpenter"      "Clark Gillies"     
## [190] "Dave Gagner"        "Todd Bertuzzi"      "Justin Williams"   
## [193] "Pavel Datsyuk"      "Don Lever"          "Petr Klima"        
## [196] "Denis Potvin"       "Petr Nedved"        "Martin Gelinas"    
## [199] "Bob Nevin"          "Daniel Briere"      "Cliff Ronning"     
## [202] "Pavol Demitra"      "Brian Sutter"       "Dennis Hull"       
## [205] "Ulf Dahlen"         "Jamie Benn"         "Brad Richards"     
## [208] "Russ Courtnall"     "George Armstrong"   "Dustin Brown"      
## [211] "Mike Cammalleri"    "Jason Pominville"   "Mike Ridley"       
## [214] "Tom Lysiak"         "Simon Gagne"        "Brian Gionta"      
## [217] "Steve Sullivan"     "Neal Broten"        "James Neal"        
## [220] "Larry Murphy"       "Pete Mahovlich"     "Patrick Sharp"     
## [223] "Tony Tanti"         "Brad Marchand"      "Brent Ashton"      
## [226] "René Robert"        "Radim Vrbata"       "Bill Goldsworthy"  
## [229] "Alex Tanguay"       "Dick Duff"          "John Anderson"     
## [232] "Bob Pulford"        "Red Kelly"          "Camille Henry"     
## [235] "Mike Knuble"        "Doug Weight"        "Mike Fisher"       
## [238] "Jim Pappin"         "Cory Stillman"      "Ralph Backstrom"   
## [241] "Max Pacioretty"     "Tyler Seguin"       "Wayne Cashman"     
## [244] "Ron Stewart"        "Jimmy Carson"       "Ron Duguay"        
## [247] "Bryan Smolinski"    "Murray Oliver"      "Shayne Corson"     
## [250] "Ryan Getzlaf"       "Al Secord"
str_detect(top_250$player, "Wayne")
##   [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [229] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [241] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
sum(str_detect(top_250$player, "Wayne"))
## [1] 2
top_250 %>%
    summarise(num_Wayne = sum(str_detect(player, "Wayne")))
## # A tibble: 1 × 1
##   num_Wayne
##       <int>
## 1         2

Extract matches

top_250 %>% 
    mutate(col_Wayne = str_extract(player, "Wayne")) %>% 
    select(player, col_Wayne) %>%
    filter(is.na(col_Wayne))
## # A tibble: 249 × 2
##    player        col_Wayne
##    <chr>         <chr>    
##  1 Gordie Howe   <NA>     
##  2 Jaromir Jagr  <NA>     
##  3 Brett Hull    <NA>     
##  4 Marcel Dionne <NA>     
##  5 Phil Esposito <NA>     
##  6 Mike Gartner  <NA>     
##  7 Alex Ovechkin <NA>     
##  8 Mark Messier  <NA>     
##  9 Steve Yzerman <NA>     
## 10 Mario Lemieux <NA>     
## # ℹ 239 more rows

Replacing matches

top_250 %>%
    mutate(col_Brett = str_replace(player, "Wayne", "Brett")) %>% 
    select(player, col_Brett)
## # A tibble: 251 × 2
##    player        col_Brett    
##    <chr>         <chr>        
##  1 Wayne Gretzky Brett Gretzky
##  2 Gordie Howe   Gordie Howe  
##  3 Jaromir Jagr  Jaromir Jagr 
##  4 Brett Hull    Brett Hull   
##  5 Marcel Dionne Marcel Dionne
##  6 Phil Esposito Phil Esposito
##  7 Mike Gartner  Mike Gartner 
##  8 Alex Ovechkin Alex Ovechkin
##  9 Mark Messier  Mark Messier 
## 10 Steve Yzerman Steve Yzerman
## # ℹ 241 more rows