Import your data

fires <- readxl::read_excel("myData.xlsx")

Chapter 14

Tools

Detect matches

fires %>%
    summarise(sum(str_detect(DAMAGE_COSTS, "9$")))
## # A tibble: 1 × 1
##   `sum(str_detect(DAMAGE_COSTS, "9$"))`
##                                   <int>
## 1                                     7
str_detect(fires$DAMAGE_COSTS, "9$")
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [49] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE
## [61] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
sum(str_detect(fires$DAMAGE_COSTS, "9$"))
## [1] 7
mean(str_detect(fires$DAMAGE_COSTS, "9$"))
## [1] 0.08433735
fires %>%
    summarise(sum(str_detect(SEVERITY, "Severe")))
## # A tibble: 1 × 1
##   `sum(str_detect(SEVERITY, "Severe"))`
##                                   <int>
## 1                                     9
str_detect(fires$SEVERITY, "Severe")
##  [1] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
## [13]  TRUE FALSE FALSE FALSE FALSE  TRUE 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 FALSE FALSE FALSE FALSE
## [73] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
sum(str_detect(fires$SEVERITY, "Severe"))
## [1] 9

Extract matches

fires %>%
    mutate(col_Severe = str_extract(SEVERITY, "Severe")) %>%
    select(SEVERITY, col_Severe) %>%
    filter(!is.na(col_Severe))
## # A tibble: 9 × 2
##   SEVERITY col_Severe
##   <chr>    <chr>     
## 1 Severe   Severe    
## 2 Severe   Severe    
## 3 Severe   Severe    
## 4 Severe   Severe    
## 5 Severe   Severe    
## 6 Severe   Severe    
## 7 Severe   Severe    
## 8 Severe   Severe    
## 9 Severe   Severe

Replacing matches

fires %>%
    mutate(col_Bad = str_replace(SEVERITY, "Severe", "Bad")) %>%
    select(SEVERITY, col_Bad)
## # A tibble: 83 × 2
##    SEVERITY col_Bad 
##    <chr>    <chr>   
##  1 Moderate Moderate
##  2 Severe   Bad     
##  3 Moderate Moderate
##  4 Severe   Bad     
##  5 Mild     Mild    
##  6 Moderate Moderate
##  7 Severe   Bad     
##  8 Moderate Moderate
##  9 Moderate Moderate
## 10 Moderate Moderate
## # ℹ 73 more rows