Import your data

data <- read.csv("../00_data/MyData.csv")

Chapter 14

Tools

Detect matches

data$County
##  [1] "Ada"               "Addison"           "Adrian"           
##  [4] "Adrian"            "Albion"            "Albion"           
##  [7] "Algoma Township"   "Algonac"           "Allegan"          
## [10] "Allegan"           "Allegan"           "Alma"             
## [13] "Alpena"            "Ann Arbor"         "Ann Arbor"        
## [16] "Assininns"         "Atlanta"           "Augusta"          
## [19] "Battle Creek"      "Battle Creek"      "Bay City"         
## [22] "Bay City"          "Bay City"          "Bellaire"         
## [25] "Dearborn"          "Dearborn"          "Dearborn"         
## [28] "Belding"           "Belleville"        "Belleville"       
## [31] "Belleville"        "Belleville"        "Berville"         
## [34] "Big Rapids"        "Big Rapids"        "Big Rapids"       
## [37] "Birmingham"        "Bloomfield"        "Bloomfield"       
## [40] "Borculo"           "Breckenridge"      "Bridgeport"       
## [43] "Bridgeport"        "Brighton"          "Brimley"          
## [46] "Brooklyn"          "Brownstown"        "Brownstown"       
## [49] "Brownstown"        "Burton"            "Byron"            
## [52] "Calumet"           "Canton"            "Canton"           
## [55] "Canton"            "Canton"            "Cass City"        
## [58] "Central Lake"      "Charlevoix"        "Charlotte"        
## [61] "Charlotte"         "Cheboygan"         "Cheboygan"        
## [64] "Cheboygan"         "Chelsea"           "Chelsea"          
## [67] "Chelsea"           "Chesaning"         "Clarksdale"       
## [70] "Clarkston"         "Clarkston"         "Clinton Township" 
## [73] "Clinton Township"  "Clinton Township"  "Clinton County"   
## [76] "Clinton County"    "Clinton Township"  "Clyde township"   
## [79] "Cockeysville"      "Coldwater"         "Coldwater"        
## [82] "Croton"            "Cranbrooke campus" "Crystal Falls"    
## [85] "Dansville"         "Davison"           "Dearborn"         
## [88] "Dearborn"          "Dearborn"          "Dearborn"         
## [91] "Dearborn"          "Dearborn"          "Dearborn"         
## [94] "Dearborn"          "Dearborn"          "Dearborn"         
## [97] "Dearborn Heights"  "Dearborn Heights"
str_detect(data$County, "Battle Creek")
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE 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
data %>%
    summarise(num_Chelsea = sum(str_detect(County, "Chelsea")))
##   num_Chelsea
## 1           3

Extract matches

data %>%
    mutate(col_Chelsea = str_extract(County, "Chelsea")) %>%
    select(County, col_Chelsea) %>%
    filter(!is.na(col_Chelsea))
##    County col_Chelsea
## 1 Chelsea     Chelsea
## 2 Chelsea     Chelsea
## 3 Chelsea     Chelsea

Replacing matches

data %>%
    mutate(col_Dearborn = str_replace(County, "Chelsea", "Dearborn")) %>%
    select(County, col_Dearborn)
##               County      col_Dearborn
## 1                Ada               Ada
## 2            Addison           Addison
## 3             Adrian            Adrian
## 4             Adrian            Adrian
## 5             Albion            Albion
## 6             Albion            Albion
## 7    Algoma Township   Algoma Township
## 8            Algonac           Algonac
## 9            Allegan           Allegan
## 10           Allegan           Allegan
## 11           Allegan           Allegan
## 12              Alma              Alma
## 13            Alpena            Alpena
## 14         Ann Arbor         Ann Arbor
## 15         Ann Arbor         Ann Arbor
## 16         Assininns         Assininns
## 17           Atlanta           Atlanta
## 18           Augusta           Augusta
## 19      Battle Creek      Battle Creek
## 20      Battle Creek      Battle Creek
## 21          Bay City          Bay City
## 22          Bay City          Bay City
## 23          Bay City          Bay City
## 24          Bellaire          Bellaire
## 25          Dearborn          Dearborn
## 26          Dearborn          Dearborn
## 27          Dearborn          Dearborn
## 28           Belding           Belding
## 29        Belleville        Belleville
## 30        Belleville        Belleville
## 31        Belleville        Belleville
## 32        Belleville        Belleville
## 33          Berville          Berville
## 34        Big Rapids        Big Rapids
## 35        Big Rapids        Big Rapids
## 36        Big Rapids        Big Rapids
## 37        Birmingham        Birmingham
## 38        Bloomfield        Bloomfield
## 39        Bloomfield        Bloomfield
## 40           Borculo           Borculo
## 41      Breckenridge      Breckenridge
## 42        Bridgeport        Bridgeport
## 43        Bridgeport        Bridgeport
## 44          Brighton          Brighton
## 45           Brimley           Brimley
## 46          Brooklyn          Brooklyn
## 47        Brownstown        Brownstown
## 48        Brownstown        Brownstown
## 49        Brownstown        Brownstown
## 50            Burton            Burton
## 51             Byron             Byron
## 52           Calumet           Calumet
## 53            Canton            Canton
## 54            Canton            Canton
## 55            Canton            Canton
## 56            Canton            Canton
## 57         Cass City         Cass City
## 58      Central Lake      Central Lake
## 59        Charlevoix        Charlevoix
## 60         Charlotte         Charlotte
## 61         Charlotte         Charlotte
## 62         Cheboygan         Cheboygan
## 63         Cheboygan         Cheboygan
## 64         Cheboygan         Cheboygan
## 65           Chelsea          Dearborn
## 66           Chelsea          Dearborn
## 67           Chelsea          Dearborn
## 68         Chesaning         Chesaning
## 69        Clarksdale        Clarksdale
## 70         Clarkston         Clarkston
## 71         Clarkston         Clarkston
## 72  Clinton Township  Clinton Township
## 73  Clinton Township  Clinton Township
## 74  Clinton Township  Clinton Township
## 75    Clinton County    Clinton County
## 76    Clinton County    Clinton County
## 77  Clinton Township  Clinton Township
## 78    Clyde township    Clyde township
## 79      Cockeysville      Cockeysville
## 80         Coldwater         Coldwater
## 81         Coldwater         Coldwater
## 82            Croton            Croton
## 83 Cranbrooke campus Cranbrooke campus
## 84     Crystal Falls     Crystal Falls
## 85         Dansville         Dansville
## 86           Davison           Davison
## 87          Dearborn          Dearborn
## 88          Dearborn          Dearborn
## 89          Dearborn          Dearborn
## 90          Dearborn          Dearborn
## 91          Dearborn          Dearborn
## 92          Dearborn          Dearborn
## 93          Dearborn          Dearborn
## 94          Dearborn          Dearborn
## 95          Dearborn          Dearborn
## 96          Dearborn          Dearborn
## 97  Dearborn Heights  Dearborn Heights
## 98  Dearborn Heights  Dearborn Heights