Import your data

emperors <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-08-13/emperors.csv")
## Rows: 68 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (11): name, name_full, birth_cty, birth_prv, rise, cause, killer, dynas...
## dbl   (1): index
## date  (4): birth, death, reign_start, reign_end
## 
## ℹ 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

emperors$name
##  [1] "Augustus"              "Tiberius"              "Caligula"             
##  [4] "Claudius"              "Nero"                  "Galba"                
##  [7] "Otho"                  "Vitellius"             "Vespasian"            
## [10] "Titus"                 "Domitian"              "Nerva"                
## [13] "Trajan"                "Hadrian"               "Antonius Pius"        
## [16] "Marcus Aurelius"       "Lucius Verus"          "Commodus"             
## [19] "Pertinax"              "Didius Julianus"       "Septimus Severus"     
## [22] "Caracalla"             "Geta"                  "Macrinus"             
## [25] "Elagabalus"            "Severus Alexander"     "Maximinus I"          
## [28] "Gordian I"             "Gordian II"            "Pupienus"             
## [31] "Balbinus"              "Gordian III"           "Philip I"             
## [34] "Trajan Decius"         "Hostilian"             "Trebonianus Gallus"   
## [37] "Aemilian"              "Valerian"              "Gallienus"            
## [40] "Claudius Gothicus"     "Quintillus"            "Aurelian"             
## [43] "Tacitus"               "Florian"               "Probus"               
## [46] "Carus"                 "Numerian"              "Carinus"              
## [49] "Diocletian"            "Maximian"              "Constantius I"        
## [52] "Galerius"              "Severus II"            "Constantine the Great"
## [55] "Maxentius"             "Maximinus II"          "Lucinius I"           
## [58] "Constantine II"        "Consantius II"         "Constans"             
## [61] "Vetranio"              "Julian"                "Jovian"               
## [64] "Valentinian I"         "Valens"                "Gratian"              
## [67] "Valentinian II"        "Theodosius I"
str_detect(emperors$name, "Augustus") 
##  [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
sum(str_detect(emperors$name, "Augustus") )
## [1] 1
emperors %>%
    summarise(num_Augustus = sum(str_detect(name, "Augustus")))
## # A tibble: 1 × 1
##   num_Augustus
##          <int>
## 1            1

Extract matches

emperors %>%
    mutate(col_Augustus = str_extract(name, "Augustus")) %>%
    select(name, col_Augustus) %>%
    filter(!is.na(col_Augustus))
## # A tibble: 1 × 2
##   name     col_Augustus
##   <chr>    <chr>       
## 1 Augustus Augustus

Replacing matches

emperors %>%
    mutate(col_caligula = str_replace(name, "augustus", "caligula")) %>%
    select(name, col_caligula)
## # A tibble: 68 × 2
##    name      col_caligula
##    <chr>     <chr>       
##  1 Augustus  Augustus    
##  2 Tiberius  Tiberius    
##  3 Caligula  Caligula    
##  4 Claudius  Claudius    
##  5 Nero      Nero        
##  6 Galba     Galba       
##  7 Otho      Otho        
##  8 Vitellius Vitellius   
##  9 Vespasian Vespasian   
## 10 Titus     Titus       
## # ℹ 58 more rows