Import your data

survivalists <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-01-24/survivalists.csv')
## Rows: 94 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): name, gender, city, state, country, reason_tapped_out, reason_cate...
## dbl  (5): season, age, result, days_lasted, day_linked_up
## lgl  (1): medically_evacuated
## 
## ℹ 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.
set.seed(1234)
survivalists_small <- survivalists %>% select(1:3) %>% sample_n(10)

survivalists_small
## # A tibble: 10 × 3
##    season name                 age
##     <dbl> <chr>              <dbl>
##  1      3 Britt Ahart           40
##  2      8 Nate Weber            47
##  3      3 Carleigh Fairchild    28
##  4      1 Chris Weatherman      41
##  5      1 Dustin Feher          37
##  6      4 Brody Wilkes          33
##  7      2 Randy Champagne       28
##  8      1 Lucas Miller          32
##  9      9 Karie Lee Knoke       57
## 10      7 Joe Nicholas          31

Chapter 14

Tools

Detect matches

survivalists_small %>%
    summarise(sum(str_detect(season, "1$")))
## # A tibble: 1 × 1
##   `sum(str_detect(season, "1$"))`
##                             <int>
## 1                               3
str_detect(survivalists_small$season, "1$")
##  [1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE
sum(str_detect(survivalists_small$season, "1$"))
## [1] 3
mean(str_detect(survivalists_small$season, "1$"))
## [1] 0.3

Extract matches

age <- c("40", "47", "28", "41", "37", "33", "28", "32", "57")
age_match <- str_c(age, collapse = "|")

str_extract(survivalists_small, age_match) 
## Warning in stri_extract_first_regex(string, pattern, opts_regex =
## opts(pattern)): argument is not an atomic vector; coercing
## [1] NA   NA   "40"

Replacing matches

survivalists_small %>% mutate(name_rev = name %>% str_replace("^[A-Z]", "="))
## # A tibble: 10 × 4
##    season name                 age name_rev          
##     <dbl> <chr>              <dbl> <chr>             
##  1      3 Britt Ahart           40 =ritt Ahart       
##  2      8 Nate Weber            47 =ate Weber        
##  3      3 Carleigh Fairchild    28 =arleigh Fairchild
##  4      1 Chris Weatherman      41 =hris Weatherman  
##  5      1 Dustin Feher          37 =ustin Feher      
##  6      4 Brody Wilkes          33 =rody Wilkes      
##  7      2 Randy Champagne       28 =andy Champagne   
##  8      1 Lucas Miller          32 =ucas Miller      
##  9      9 Karie Lee Knoke       57 =arie Lee Knoke   
## 10      7 Joe Nicholas          31 =oe Nicholas
survivalists_small %>% mutate(name_rev = name %>% str_replace_all("^[A-Z]", "="))
## # A tibble: 10 × 4
##    season name                 age name_rev          
##     <dbl> <chr>              <dbl> <chr>             
##  1      3 Britt Ahart           40 =ritt Ahart       
##  2      8 Nate Weber            47 =ate Weber        
##  3      3 Carleigh Fairchild    28 =arleigh Fairchild
##  4      1 Chris Weatherman      41 =hris Weatherman  
##  5      1 Dustin Feher          37 =ustin Feher      
##  6      4 Brody Wilkes          33 =rody Wilkes      
##  7      2 Randy Champagne       28 =andy Champagne   
##  8      1 Lucas Miller          32 =ucas Miller      
##  9      9 Karie Lee Knoke       57 =arie Lee Knoke   
## 10      7 Joe Nicholas          31 =oe Nicholas
survivalists_small %>% mutate(season_rev = season %>% str_replace("1", "-"))
## # A tibble: 10 × 4
##    season name                 age season_rev
##     <dbl> <chr>              <dbl> <chr>     
##  1      3 Britt Ahart           40 3         
##  2      8 Nate Weber            47 8         
##  3      3 Carleigh Fairchild    28 3         
##  4      1 Chris Weatherman      41 -         
##  5      1 Dustin Feher          37 -         
##  6      4 Brody Wilkes          33 4         
##  7      2 Randy Champagne       28 2         
##  8      1 Lucas Miller          32 -         
##  9      9 Karie Lee Knoke       57 9         
## 10      7 Joe Nicholas          31 7
survivalists_small %>% mutate(name_rev = name %>% str_replace_all(" ", "-"))
## # A tibble: 10 × 4
##    season name                 age name_rev          
##     <dbl> <chr>              <dbl> <chr>             
##  1      3 Britt Ahart           40 Britt-Ahart       
##  2      8 Nate Weber            47 Nate-Weber        
##  3      3 Carleigh Fairchild    28 Carleigh-Fairchild
##  4      1 Chris Weatherman      41 Chris-Weatherman  
##  5      1 Dustin Feher          37 Dustin-Feher      
##  6      4 Brody Wilkes          33 Brody-Wilkes      
##  7      2 Randy Champagne       28 Randy-Champagne   
##  8      1 Lucas Miller          32 Lucas-Miller      
##  9      9 Karie Lee Knoke       57 Karie-Lee-Knoke   
## 10      7 Joe Nicholas          31 Joe-Nicholas
survivalists_small %>% mutate(name_rev = name %>% str_replace_all(" ", "|"))
## # A tibble: 10 × 4
##    season name                 age name_rev          
##     <dbl> <chr>              <dbl> <chr>             
##  1      3 Britt Ahart           40 Britt|Ahart       
##  2      8 Nate Weber            47 Nate|Weber        
##  3      3 Carleigh Fairchild    28 Carleigh|Fairchild
##  4      1 Chris Weatherman      41 Chris|Weatherman  
##  5      1 Dustin Feher          37 Dustin|Feher      
##  6      4 Brody Wilkes          33 Brody|Wilkes      
##  7      2 Randy Champagne       28 Randy|Champagne   
##  8      1 Lucas Miller          32 Lucas|Miller      
##  9      9 Karie Lee Knoke       57 Karie|Lee|Knoke   
## 10      7 Joe Nicholas          31 Joe|Nicholas