Import your data

stats <- read_csv("../00_data/myData.csv")
## New names:
## Rows: 39 Columns: 8
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): Country, ...8 dbl (5): Kill/Death Ratio, Player Rating, Headshot
## Percentage, Kills Per Rou... lgl (1): ...7
## ℹ 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.
## • `` -> `...7`
## • `` -> `...8`
stats <- stats %>%
    janitor::clean_names()

Chapter 14

Tools

Detect matches

# How many countries start with a vowel?
stats %>% summarise(sum(str_detect(country, "^[AEIOU]")))
## # A tibble: 1 × 1
##   `sum(str_detect(country, "^[AEIOU]"))`
##                                    <int>
## 1                                      7
# How many countries end with a vowel?
stats %>% summarise(sum(str_detect(country, "[aeiou]$")))
## # A tibble: 1 × 1
##   `sum(str_detect(country, "[aeiou]$"))`
##                                    <int>
## 1                                     19

Extract matches

vowels <- c("a","e","i","o","u")
vowels_match <- str_c(vowels, collapse = "|")

stats %>% summarise(str_extract(country, vowels_match))
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## # A tibble: 39 × 1
##    `str_extract(country, vowels_match)`
##    <chr>                               
##  1 e                                   
##  2 u                                   
##  3 e                                   
##  4 e                                   
##  5 a                                   
##  6 u                                   
##  7 a                                   
##  8 i                                   
##  9 e                                   
## 10 e                                   
## # … with 29 more rows

Replacing matches

stats %>% summarise(str_replace_all(country, "[AEIOU,aeiou]", "-"))
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## # A tibble: 39 × 1
##    `str_replace_all(country, "[AEIOU,aeiou]", "-")`
##    <chr>                                           
##  1 -rg-nt-n-                                       
##  2 --str-l--                                       
##  3 B-l-r-s                                         
##  4 B-lg--m                                         
##  5 Br-z-l                                          
##  6 B-lg-r--                                        
##  7 C-n-d-                                          
##  8 Ch-n-                                           
##  9 Cz-ch R-p-bl-c                                  
## 10 D-nm-rk                                         
## # … with 29 more rows