Import your data

myData <- read_csv("../00_data/myData.csv")
## Rows: 27 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): film, film_rating
## dbl  (2): number, run_time
## date (1): release_date
## 
## ℹ 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

myData %>%
    summarise(sum(str_detect(film,"^T")))
## # A tibble: 1 × 1
##   `sum(str_detect(film, "^T"))`
##                           <int>
## 1                            NA
str_detect(myData$film, "^T")
##  [1]  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
## [13] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
## [25]  TRUE FALSE    NA
sum(str_detect(myData$film, "^T"))
## [1] NA
mean(str_detect(myData$film, "^T"))
## [1] NA

Extract matches

    film_rating <- c("G")
rating_match <- str_c(film_rating, collapse = "|")

has_rating <- (str_subset(film_rating, rating_match))
str_extract(has_rating, rating_match)
## [1] "G"

Replacing matches

myData%>% mutate(film_T= film%>% str_replace_all("[T,t]", "-"))
## # A tibble: 27 × 6
##    number film            release_date run_time film_rating film_T         
##     <dbl> <chr>           <date>          <dbl> <chr>       <chr>          
##  1      1 Toy Story       1995-11-22         81 G           -oy S-ory      
##  2      2 A Bug's Life    1998-11-25         95 G           A Bug's Life   
##  3      3 Toy Story 2     1999-11-24         92 G           -oy S-ory 2    
##  4      4 Monsters, Inc.  2001-11-02         92 G           Mons-ers- Inc. 
##  5      5 Finding Nemo    2003-05-30        100 G           Finding Nemo   
##  6      6 The Incredibles 2004-11-05        115 PG          -he Incredibles
##  7      7 Cars            2006-06-09        117 G           Cars           
##  8      8 Ratatouille     2007-06-29        111 G           Ra-a-ouille    
##  9      9 WALL-E          2008-06-27         98 G           WALL-E         
## 10     10 Up              2009-05-29         96 PG          Up             
## # ℹ 17 more rows