Import your data

dog <- read_excel("../01_module4/data/myData.xlsx")

Chapter 14

Tools

Detect matches

dog$Breed
##   [1] "Retrievers (Labrador)"                
##   [2] "French Bulldogs"                      
##   [3] "German Shepherd Dogs"                 
##   [4] "Retrievers (Golden)"                  
##   [5] "Bulldogs"                             
##   [6] "Poodles"                              
##   [7] "Beagles"                              
##   [8] "Rottweilers"                          
##   [9] "Pointers (German Shorthaired)"        
##  [10] "Dachshunds"                           
##  [11] "Pembroke Welsh Corgis"                
##  [12] "Australian Shepherds"                 
##  [13] "Yorkshire Terriers"                   
##  [14] "Boxers"                               
##  [15] "Great Danes"                          
##  [16] "Siberian Huskies"                     
##  [17] "Cavalier King Charles Spaniels"       
##  [18] "Doberman Pinschers"                   
##  [19] "Miniature Schnauzers"                 
##  [20] "Shih Tzu"                             
##  [21] "Boston Terriers"                      
##  [22] "Bernese Mountain Dogs"                
##  [23] "Pomeranians"                          
##  [24] "Havanese"                             
##  [25] "Cane Corso"                           
##  [26] "Spaniels (English Springer)"          
##  [27] "Shetland Sheepdogs"                   
##  [28] "Brittanys"                            
##  [29] "Pugs"                                 
##  [30] "Spaniels (Cocker)"                    
##  [31] "Miniature American Shepherds"         
##  [32] "Border Collies"                       
##  [33] "Mastiffs"                             
##  [34] "Chihuahuas"                           
##  [35] "Vizslas"                              
##  [36] "Basset Hounds"                        
##  [37] "Belgian Malinois"                     
##  [38] "Maltese"                              
##  [39] "Weimaraners"                          
##  [40] "Collies"                              
##  [41] "Newfoundlands"                        
##  [42] "Rhodesian Ridgebacks"                 
##  [43] "Shiba Inu"                            
##  [44] "West Highland White Terriers"         
##  [45] "Bichons Frises"                       
##  [46] "Bloodhounds"                          
##  [47] "Spaniels (English Cocker)"            
##  [48] "Akitas"                               
##  [49] "Portuguese Water Dogs"                
##  [50] "Retrievers (Chesapeake Bay)"          
##  [51] "Dalmatians"                           
##  [52] "St. Bernards"                         
##  [53] "Papillons"                            
##  [54] "Australian Cattle Dogs"               
##  [55] "Bullmastiffs"                         
##  [56] "Samoyeds"                             
##  [57] "Scottish Terriers"                    
##  [58] "Soft Coated Wheaten Terriers"         
##  [59] "Whippets"                             
##  [60] "Pointers (German Wirehaired)"         
##  [61] "Chinese Shar-Pei"                     
##  [62] "Airedale Terriers"                    
##  [63] "Wirehaired Pointing Griffons"         
##  [64] "Bull Terriers"                        
##  [65] "Alaskan Malamutes"                    
##  [66] "Cardigan Welsh Corgis"                
##  [67] "Giant Schnauzers"                     
##  [68] "Old English Sheepdogs"                
##  [69] "Italian Greyhounds"                   
##  [70] "Great Pyrenees"                       
##  [71] "Dogues de Bordeaux"                   
##  [72] "Russell Terriers"                     
##  [73] "Cairn Terriers"                       
##  [74] "Irish Wolfhounds"                     
##  [75] "Setters (Irish)"                      
##  [76] "Greater Swiss Mountain Dogs"          
##  [77] "Miniature Pinschers"                  
##  [78] "Lhasa Apsos"                          
##  [79] "Chinese Crested"                      
##  [80] "Coton de Tulear"                      
##  [81] "Staffordshire Bull Terriers"          
##  [82] "American Staffordshire Terriers"      
##  [83] "Rat Terriers"                         
##  [84] "Chow Chows"                           
##  [85] "Anatolian Shepherd Dogs"              
##  [86] "Basenjis"                             
##  [87] "Spaniels (Boykin)"                    
##  [88] "Lagotti Romagnoli"                    
##  [89] "Brussels Griffons"                    
##  [90] "Retrievers (Nova Scotia Duck Tolling)"
##  [91] "Norwegian Elkhounds"                  
##  [92] "Standard Schnauzers"                  
##  [93] "Dogo Argentinos"                      
##  [94] "Bouviers des Flandres"                
##  [95] "Pekingese"                            
##  [96] "Keeshonden"                           
##  [97] "Border Terriers"                      
##  [98] "Leonbergers"                          
##  [99] "Tibetan Terriers"                     
## [100] "Neapolitan Mastiffs"                  
## [101] "Setters (English)"                    
## [102] "Retrievers (Flat-Coated)"             
## [103] "Borzois"                              
## [104] "Fox Terriers (Wire)"                  
## [105] "Miniature Bull Terriers"              
## [106] "Belgian Tervuren"                     
## [107] "Setters (Gordon)"                     
## [108] "Silky Terriers"                       
## [109] "Norwich Terriers"                     
## [110] "Spinoni Italiani"                     
## [111] "Japanese Chin"                        
## [112] "Welsh Terriers"                       
## [113] "Toy Fox Terriers"                     
## [114] "Schipperkes"                          
## [115] "Parson Russell Terriers"              
## [116] "Pointers"                             
## [117] "Belgian Sheepdogs"                    
## [118] "Tibetan Spaniels"                     
## [119] "American Eskimo Dogs"                 
## [120] "Irish Terriers"                       
## [121] "Beaucerons"                           
## [122] "Afghan Hounds"                        
## [123] "Boerboels"                            
## [124] "Fox Terriers (Smooth)"                
## [125] "Bearded Collies"                      
## [126] "Black Russian Terriers"               
## [127] "Black and Tan Coonhounds"             
## [128] "Spaniels (Welsh Springer)"            
## [129] "American Hairless Terriers"           
## [130] "Norfolk Terriers"                     
## [131] "Xoloitzcuintli"                       
## [132] "Manchester Terriers"                  
## [133] "Kerry Blue Terriers"                  
## [134] "Australian Terriers"                  
## [135] "Spaniels (Clumber)"                   
## [136] "Lakeland Terriers"                    
## [137] "Bluetick Coonhounds"                  
## [138] "English Toy Spaniels"                 
## [139] "German Pinschers"                     
## [140] "Tibetan Mastiffs"                     
## [141] "Bedlington Terriers"                  
## [142] "Greyhounds"                           
## [143] "Pulik"                                
## [144] "Salukis"                              
## [145] "Barbets"                              
## [146] "Redbone Coonhounds"                   
## [147] "Swedish Vallhunds"                    
## [148] "Sealyham Terriers"                    
## [149] "Spanish Water Dogs"                   
## [150] "Briards"                              
## [151] "Berger Picards"                       
## [152] "Entlebucher Mountain Dogs"            
## [153] "Treeing Walker Coonhounds"            
## [154] "Icelandic Sheepdogs"                  
## [155] "Wirehaired Vizslas"                   
## [156] "Pumik"                                
## [157] "Portuguese Podengo Pequenos"          
## [158] "Spaniels (American Water)"            
## [159] "Retrievers (Curly-Coated)"            
## [160] "Spaniels (Field)"                     
## [161] "Lowchen"                              
## [162] "Nederlandse Kooikerhondjes"           
## [163] "Affenpinschers"                       
## [164] "Petits Bassets Griffons Vendeens"     
## [165] "Finnish Lapphunds"                    
## [166] "Scottish Deerhounds"                  
## [167] "Plott Hounds"                         
## [168] "Norwegian Buhunds"                    
## [169] "Glen of Imaal Terriers"               
## [170] "Setters (Irish Red and White)"        
## [171] "Ibizan Hounds"                        
## [172] "Spaniels (Sussex)"                    
## [173] "Bergamasco Sheepdogs"                 
## [174] "Spaniels (Irish Water)"               
## [175] "Polish Lowland Sheepdogs"             
## [176] "Otterhounds"                          
## [177] "Kuvaszok"                             
## [178] "Komondorok"                           
## [179] "Cirnechi dell’Etna"                   
## [180] "Pharaoh Hounds"                       
## [181] "Dandie Dinmont Terriers"              
## [182] "Pyrenean Shepherds"                   
## [183] "Skye Terriers"                        
## [184] "Canaan Dogs"                          
## [185] "American English Coonhounds"          
## [186] "Chinooks"                             
## [187] "Finnish Spitz"                        
## [188] "Grand Basset Griffon Vendeens"        
## [189] "Sloughis"                             
## [190] "Harriers"                             
## [191] "Cesky Terriers"                       
## [192] "American Foxhounds"                   
## [193] "Azawakhs"                             
## [194] "English Foxhounds"                    
## [195] "Norwegian Lundehunds"
str_detect(dog$Breed, "Hound")
##   [1] FALSE 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  TRUE
##  [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  TRUE 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  TRUE FALSE
## [169] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [193] FALSE FALSE FALSE
sum(str_detect(dog$Breed, "Hound"))
## [1] 5
dog %>%
    summarise(num_Hound = sum(str_detect(Breed, "Hound")))
## # A tibble: 1 × 1
##   num_Hound
##       <int>
## 1         5

Extract matches

dog %>%
    mutate(col_Hound = str_extract(Breed, "Hound")) %>%
    select(Breed, col_Hound) %>%
    filter(!is.na(col_Hound))
## # A tibble: 5 × 2
##   Breed          col_Hound
##   <chr>          <chr>    
## 1 Basset Hounds  Hound    
## 2 Afghan Hounds  Hound    
## 3 Plott Hounds   Hound    
## 4 Ibizan Hounds  Hound    
## 5 Pharaoh Hounds Hound

Replacing matches

dog %>%
    mutate(col_Pup = str_replace(Breed, "Hound", "Pup")) %>%
    select(Breed, col_Pup) %>%
    filter(Breed %>% str_detect("Hound"))
## # A tibble: 5 × 2
##   Breed          col_Pup     
##   <chr>          <chr>       
## 1 Basset Hounds  Basset Pups 
## 2 Afghan Hounds  Afghan Pups 
## 3 Plott Hounds   Plott Pups  
## 4 Ibizan Hounds  Ibizan Pups 
## 5 Pharaoh Hounds Pharaoh Pups