Import your data

taylor_album_songs <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-10-17/taylor_album_songs.csv')
## Rows: 194 Columns: 29
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (7): album_name, track_name, artist, featuring, key_name, mode_name, k...
## dbl  (14): track_number, danceability, energy, key, loudness, mode, speechin...
## lgl   (4): ep, bonus_track, explicit, lyrics
## date  (4): album_release, promotional_release, single_release, track_release
## 
## ℹ 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

taylor_album_songs$album_name
##   [1] "Taylor Swift"                "Taylor Swift"               
##   [3] "Taylor Swift"                "Taylor Swift"               
##   [5] "Taylor Swift"                "Taylor Swift"               
##   [7] "Taylor Swift"                "Taylor Swift"               
##   [9] "Taylor Swift"                "Taylor Swift"               
##  [11] "Taylor Swift"                "Taylor Swift"               
##  [13] "Taylor Swift"                "Taylor Swift"               
##  [15] "Taylor Swift"                "Fearless (Taylor's Version)"
##  [17] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [19] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [21] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [23] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [25] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [27] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [29] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [31] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [33] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [35] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [37] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [39] "Fearless (Taylor's Version)" "Fearless (Taylor's Version)"
##  [41] "Fearless (Taylor's Version)" "Speak Now"                  
##  [43] "Speak Now"                   "Speak Now"                  
##  [45] "Speak Now"                   "Speak Now"                  
##  [47] "Speak Now"                   "Speak Now"                  
##  [49] "Speak Now"                   "Speak Now"                  
##  [51] "Speak Now"                   "Speak Now"                  
##  [53] "Speak Now"                   "Speak Now"                  
##  [55] "Speak Now"                   "Speak Now"                  
##  [57] "Speak Now"                   "Speak Now"                  
##  [59] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [61] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [63] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [65] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [67] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [69] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [71] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [73] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [75] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [77] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [79] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [81] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [83] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [85] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [87] "Red (Taylor's Version)"      "Red (Taylor's Version)"     
##  [89] "1989"                        "1989"                       
##  [91] "1989"                        "1989"                       
##  [93] "1989"                        "1989"                       
##  [95] "1989"                        "1989"                       
##  [97] "1989"                        "1989"                       
##  [99] "1989"                        "1989"                       
## [101] "1989"                        "1989"                       
## [103] "1989"                        "1989"                       
## [105] "reputation"                  "reputation"                 
## [107] "reputation"                  "reputation"                 
## [109] "reputation"                  "reputation"                 
## [111] "reputation"                  "reputation"                 
## [113] "reputation"                  "reputation"                 
## [115] "reputation"                  "reputation"                 
## [117] "reputation"                  "reputation"                 
## [119] "reputation"                  "Lover"                      
## [121] "Lover"                       "Lover"                      
## [123] "Lover"                       "Lover"                      
## [125] "Lover"                       "Lover"                      
## [127] "Lover"                       "Lover"                      
## [129] "Lover"                       "Lover"                      
## [131] "Lover"                       "Lover"                      
## [133] "Lover"                       "Lover"                      
## [135] "Lover"                       "Lover"                      
## [137] "Lover"                       "folklore"                   
## [139] "folklore"                    "folklore"                   
## [141] "folklore"                    "folklore"                   
## [143] "folklore"                    "folklore"                   
## [145] "folklore"                    "folklore"                   
## [147] "folklore"                    "folklore"                   
## [149] "folklore"                    "folklore"                   
## [151] "folklore"                    "folklore"                   
## [153] "folklore"                    "folklore"                   
## [155] "evermore"                    "evermore"                   
## [157] "evermore"                    "evermore"                   
## [159] "evermore"                    "evermore"                   
## [161] "evermore"                    "evermore"                   
## [163] "evermore"                    "evermore"                   
## [165] "evermore"                    "evermore"                   
## [167] "evermore"                    "evermore"                   
## [169] "evermore"                    "evermore"                   
## [171] "evermore"                    "Midnights"                  
## [173] "Midnights"                   "Midnights"                  
## [175] "Midnights"                   "Midnights"                  
## [177] "Midnights"                   "Midnights"                  
## [179] "Midnights"                   "Midnights"                  
## [181] "Midnights"                   "Midnights"                  
## [183] "Midnights"                   "Midnights"                  
## [185] "Midnights"                   "Midnights"                  
## [187] "Midnights"                   "Midnights"                  
## [189] "Midnights"                   "Midnights"                  
## [191] "Midnights"                   "Midnights"                  
## [193] "Midnights"                   "Midnights"
str_detect(taylor_album_songs$album_name, "reputation")
##   [1] FALSE 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 FALSE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
## [109]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
## [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [193] FALSE FALSE
sum(str_detect(taylor_album_songs$album_name, "reputation"))
## [1] 15
taylor_album_songs %>%
    summarise(num_reputation = sum(str_detect(album_name, "reputation")))
## # A tibble: 1 × 1
##   num_reputation
##            <int>
## 1             15

Extract matches

taylor_album_songs %>%
    mutate(col_reputation = str_extract(album_name, "reputation")) %>%
    select(album_name, col_reputation) %>%
    filter(!is.na(col_reputation))
## # A tibble: 15 × 2
##    album_name col_reputation
##    <chr>      <chr>         
##  1 reputation reputation    
##  2 reputation reputation    
##  3 reputation reputation    
##  4 reputation reputation    
##  5 reputation reputation    
##  6 reputation reputation    
##  7 reputation reputation    
##  8 reputation reputation    
##  9 reputation reputation    
## 10 reputation reputation    
## 11 reputation reputation    
## 12 reputation reputation    
## 13 reputation reputation    
## 14 reputation reputation    
## 15 reputation reputation

Replacing matches

taylor_album_songs %>% mutate(col_Lover = str_replace(album_name, "reputation", "Lover")) %>% select(album_name, col_Lover) %>%
filter(album_name %>% str_detect("reputation"))
## # A tibble: 15 × 2
##    album_name col_Lover
##    <chr>      <chr>    
##  1 reputation Lover    
##  2 reputation Lover    
##  3 reputation Lover    
##  4 reputation Lover    
##  5 reputation Lover    
##  6 reputation Lover    
##  7 reputation Lover    
##  8 reputation Lover    
##  9 reputation Lover    
## 10 reputation Lover    
## 11 reputation Lover    
## 12 reputation Lover    
## 13 reputation Lover    
## 14 reputation Lover    
## 15 reputation Lover