Import your data

data <- read_excel("../00_data/myData.xlsx")

Chapter 14

Tools

Detect matches

data %>%
    select(pokemon) %>%
    filter(str_detect(pokemon, "mega"))
## # A tibble: 50 × 1
##    pokemon         
##    <chr>           
##  1 meganium        
##  2 yanmega         
##  3 venusaur-mega   
##  4 charizard-mega-x
##  5 charizard-mega-y
##  6 blastoise-mega  
##  7 alakazam-mega   
##  8 gengar-mega     
##  9 kangaskhan-mega 
## 10 pinsir-mega     
## # ℹ 40 more rows
data %>%
    select(pokemon) %>%
    filter(str_detect(pokemon, "-"))
## # A tibble: 181 × 1
##    pokemon         
##    <chr>           
##  1 nidoran-f       
##  2 nidoran-m       
##  3 mr-mime         
##  4 ho-oh           
##  5 deoxys-normal   
##  6 wormadam-plant  
##  7 mime-jr         
##  8 porygon-z       
##  9 giratina-altered
## 10 shaymin-land    
## # ℹ 171 more rows

Extract matches

data %>%
    select(pokemon) %>%
    mutate(base_name = str_extract(pokemon, "^[^-]+")) %>%
    filter(pokemon != base_name)
## # A tibble: 181 × 2
##    pokemon          base_name
##    <chr>            <chr>    
##  1 nidoran-f        nidoran  
##  2 nidoran-m        nidoran  
##  3 mr-mime          mr       
##  4 ho-oh            ho       
##  5 deoxys-normal    deoxys   
##  6 wormadam-plant   wormadam 
##  7 mime-jr          mime     
##  8 porygon-z        porygon  
##  9 giratina-altered giratina 
## 10 shaymin-land     shaymin  
## # ℹ 171 more rows
data %>%
    select(pokemon) %>%
    filter(str_detect(pokemon, "-")) %>%
    mutate(form = str_extract(pokemon, "(?<=-).*"))
## # A tibble: 181 × 2
##    pokemon          form   
##    <chr>            <chr>  
##  1 nidoran-f        f      
##  2 nidoran-m        m      
##  3 mr-mime          mime   
##  4 ho-oh            oh     
##  5 deoxys-normal    normal 
##  6 wormadam-plant   plant  
##  7 mime-jr          jr     
##  8 porygon-z        z      
##  9 giratina-altered altered
## 10 shaymin-land     land   
## # ℹ 171 more rows

Replacing matches

data %>%
    select(pokemon) %>%
    mutate(clean_pokemon = pokemon %>%
         str_replace_all("-", " ") %>%
         str_replace_all("mega", "") %>%
         str_squish()) %>%
    filter(pokemon !=clean_pokemon)
## # A tibble: 183 × 2
##    pokemon        clean_pokemon 
##    <chr>          <chr>         
##  1 nidoran-f      nidoran f     
##  2 nidoran-m      nidoran m     
##  3 mr-mime        mr mime       
##  4 meganium       nium          
##  5 ho-oh          ho oh         
##  6 deoxys-normal  deoxys normal 
##  7 wormadam-plant wormadam plant
##  8 mime-jr        mime jr       
##  9 yanmega        yan           
## 10 porygon-z      porygon z     
## # ℹ 173 more rows