Import your data

data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-17/office_ratings.csv')
## Rows: 188 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): title
## dbl  (4): season, episode, imdb_rating, total_votes
## date (1): air_date
## 
## ℹ 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

data$title
##   [1] "Pilot"                              
##   [2] "Diversity Day"                      
##   [3] "Health Care"                        
##   [4] "The Alliance"                       
##   [5] "Basketball"                         
##   [6] "Hot Girl"                           
##   [7] "The Dundies"                        
##   [8] "Sexual Harassment"                  
##   [9] "Office Olympics"                    
##  [10] "The Fire"                           
##  [11] "Halloween"                          
##  [12] "The Fight"                          
##  [13] "The Client"                         
##  [14] "Performance Review"                 
##  [15] "E-Mail Surveillance"                
##  [16] "Christmas Party"                    
##  [17] "Booze Cruise"                       
##  [18] "The Injury"                         
##  [19] "The Secret"                         
##  [20] "The Carpet"                         
##  [21] "Boys and Girls"                     
##  [22] "Valentine's Day"                    
##  [23] "Dwight's Speech"                    
##  [24] "Take Your Daughter to Work Day"     
##  [25] "Michael's Birthday"                 
##  [26] "Drug Testing"                       
##  [27] "Conflict Resolution"                
##  [28] "Casino Night"                       
##  [29] "Gay Witch Hunt"                     
##  [30] "The Convention"                     
##  [31] "The Coup"                           
##  [32] "Grief Counseling"                   
##  [33] "Initiation"                         
##  [34] "Diwali"                             
##  [35] "Branch Closing"                     
##  [36] "The Merger"                         
##  [37] "The Convict"                        
##  [38] "A Benihana Christmas"               
##  [39] "Back from Vacation"                 
##  [40] "Traveling Salesmen"                 
##  [41] "The Return"                         
##  [42] "Ben Franklin"                       
##  [43] "Phyllis' Wedding"                   
##  [44] "Business School"                    
##  [45] "Cocktails"                          
##  [46] "The Negotiation"                    
##  [47] "Safety Training"                    
##  [48] "Product Recall"                     
##  [49] "Women's Appreciation"               
##  [50] "Beach Games"                        
##  [51] "The Job"                            
##  [52] "Fun Run"                            
##  [53] "Dunder Mifflin Infinity"            
##  [54] "Launch Party"                       
##  [55] "Money"                              
##  [56] "Local Ad"                           
##  [57] "Branch Wars"                        
##  [58] "Survivor Man"                       
##  [59] "The Deposition"                     
##  [60] "Dinner Party"                       
##  [61] "Chair Model"                        
##  [62] "Night Out"                          
##  [63] "Did I Stutter?"                     
##  [64] "Job Fair"                           
##  [65] "Goodbye, Toby"                      
##  [66] "Weight Loss"                        
##  [67] "Business Ethics"                    
##  [68] "Baby Shower"                        
##  [69] "Crime Aid"                          
##  [70] "Employee Transfer"                  
##  [71] "Customer Survey"                    
##  [72] "Business Trip"                      
##  [73] "Frame Toby"                         
##  [74] "The Surplus"                        
##  [75] "Moroccan Christmas"                 
##  [76] "The Duel"                           
##  [77] "Prince Family Paper"                
##  [78] "Stress Relief"                      
##  [79] "Lecture Circuit: Part 1"            
##  [80] "Lecture Circuit: Part 2"            
##  [81] "Blood Drive"                        
##  [82] "Golden Ticket"                      
##  [83] "New Boss"                           
##  [84] "Two Weeks"                          
##  [85] "Dream Team"                         
##  [86] "Michael Scott Paper Company"        
##  [87] "Heavy Competition"                  
##  [88] "Broke"                              
##  [89] "Casual Friday"                      
##  [90] "Cafe Disco"                         
##  [91] "Company Picnic"                     
##  [92] "Gossip"                             
##  [93] "The Meeting"                        
##  [94] "The Promotion"                      
##  [95] "Niagara: Part 1"                    
##  [96] "Niagara: Part 2"                    
##  [97] "Mafia"                              
##  [98] "The Lover"                          
##  [99] "Koi Pond"                           
## [100] "Double Date"                        
## [101] "Murder"                             
## [102] "Shareholder Meeting"                
## [103] "Scott's Tots"                       
## [104] "Secret Santa"                       
## [105] "The Banker"                         
## [106] "Sabre"                              
## [107] "Manager and Salesman"               
## [108] "The Delivery: Part 1"               
## [109] "The Delivery: Part 2"               
## [110] "St. Patrick's Day"                  
## [111] "New Leads"                          
## [112] "Happy Hour"                         
## [113] "Secretary's Day"                    
## [114] "Body Language"                      
## [115] "The Cover-Up"                       
## [116] "The Chump"                          
## [117] "Whistleblower"                      
## [118] "Nepotism"                           
## [119] "Counseling"                         
## [120] "Andy's Play"                        
## [121] "Sex Ed"                             
## [122] "The Sting"                          
## [123] "Costume Contest"                    
## [124] "Christening"                        
## [125] "Viewing Party"                      
## [126] "WUPHF.com"                          
## [127] "China"                              
## [128] "Classy Christmas"                   
## [129] "Ultimatum"                          
## [130] "The Seminar"                        
## [131] "The Search"                         
## [132] "PDA"                                
## [133] "Threat Level Midnight"              
## [134] "Todd Packer"                        
## [135] "Garage Sale"                        
## [136] "Training Day"                       
## [137] "Michael's Last Dundies"             
## [138] "Goodbye, Michael"                   
## [139] "The Inner Circle"                   
## [140] "Dwight K. Schrute, (Acting) Manager"
## [141] "Search Committee"                   
## [142] "The List"                           
## [143] "The Incentive"                      
## [144] "Lotto"                              
## [145] "Garden Party"                       
## [146] "Spooked"                            
## [147] "Doomsday"                           
## [148] "Pam's Replacement"                  
## [149] "Gettysburg"                         
## [150] "Mrs. California"                    
## [151] "Christmas Wishes"                   
## [152] "Trivia"                             
## [153] "Pool Party"                         
## [154] "Jury Duty"                          
## [155] "Special Project"                    
## [156] "Tallahassee"                        
## [157] "After Hours"                        
## [158] "Test the Store"                     
## [159] "Last Day in Florida"                
## [160] "Get the Girl"                       
## [161] "Welcome Party"                      
## [162] "Angry Andy"                         
## [163] "Fundraiser"                         
## [164] "Turf War"                           
## [165] "Free Family Portrait Studio"        
## [166] "New Guys"                           
## [167] "Roy's Wedding"                      
## [168] "Andy's Ancestry"                    
## [169] "Work Bus"                           
## [170] "Here Comes Treble"                  
## [171] "The Boat"                           
## [172] "The Whale"                          
## [173] "The Target"                         
## [174] "Dwight Christmas"                   
## [175] "Lice"                               
## [176] "Suit Warehouse"                     
## [177] "Customer Loyalty"                   
## [178] "Junior Salesman"                    
## [179] "Vandalism"                          
## [180] "Couples Discount"                   
## [181] "Moving On"                          
## [182] "The Farm"                           
## [183] "Promos"                             
## [184] "Stairmageddon"                      
## [185] "Paper Airplane"                     
## [186] "Livin' the Dream"                   
## [187] "A.A.R.M."                           
## [188] "Finale"
str_detect(data$title, "The")
##   [1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
##  [13]  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
##  [37]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
##  [49] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [97] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
## [109]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
## [121] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE 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  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
sum(str_detect(data$title, "The"))
## [1] 36
data %>% 
    summarise(num_The = sum(str_detect(title, "The")))
## # A tibble: 1 × 1
##   num_The
##     <int>
## 1      36

Extract matches

data %>% 
    mutate(col_The = str_extract(title, "The")) %>% 
    select(title, col_The) %>% 
    filter(!is.na(col_The))
## # A tibble: 36 × 2
##    title          col_The
##    <chr>          <chr>  
##  1 The Alliance   The    
##  2 The Dundies    The    
##  3 The Fire       The    
##  4 The Fight      The    
##  5 The Client     The    
##  6 The Injury     The    
##  7 The Secret     The    
##  8 The Carpet     The    
##  9 The Convention The    
## 10 The Coup       The    
## # ℹ 26 more rows

Replacing matches

data %>% 
    mutate(col_Office = str_replace(title, "The", "Office")) %>% 
    select(title, col_Office)
## # A tibble: 188 × 2
##    title             col_Office       
##    <chr>             <chr>            
##  1 Pilot             Pilot            
##  2 Diversity Day     Diversity Day    
##  3 Health Care       Health Care      
##  4 The Alliance      Office Alliance  
##  5 Basketball        Basketball       
##  6 Hot Girl          Hot Girl         
##  7 The Dundies       Office Dundies   
##  8 Sexual Harassment Sexual Harassment
##  9 Office Olympics   Office Olympics  
## 10 The Fire          Office Fire      
## # ℹ 178 more rows