Import your data

data <- read_csv("../00_data/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.

Chapter 14

Tools

Make Data Small

data_small <- data %>%
    select(name, reason_tapped_out, days_lasted, season) %>%
    sample_n(10)
data_small
## # A tibble: 10 × 4
##    name                    reason_tapped_out              days_lasted season
##    <chr>                   <chr>                                <dbl>  <dbl>
##  1 Tracy Wilson            Afraid of bears                          8      2
##  2 Zachary Gault           Cut arm with axe                         8      3
##  3 Kielyn Marrone          Starvation                              80      7
##  4 Callie North            Felt like journey was complete          72      3
##  5 Wayne Russell           Fear of bears                            4      1
##  6 Brad Richardson         Had no food the whole time               7      5
##  7 Jacques Turcotte        Missed his family                       15      9
##  8 Callie Russell          Frostbite of the toes                   89      7
##  9 Theresa Emmerich Kamper Low BMI, lost too much weight           69      8
## 10 Tim Madsen              Anxiety attack, chest pains              6      8

Detect matches

data %>% 
    summarise(sum(str_detect(season, "6$")))
## # A tibble: 1 × 1
##   `sum(str_detect(season, "6$"))`
##                             <int>
## 1                              10
str_detect(data_small$days_lasted, "6$")
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
sum(str_detect(data_small$days_lasted, "6$"))
## [1] 1
mean(str_detect(data_small$season, "7$"))
## [1] 0.2

Extract matches

data <- c("12", "8", "7", "2", "1", "0")
data_match <- str_c(data, collapse = "|")
data_match
## [1] "12|8|7|2|1|0"

Replacing matches

data_small %>% mutate(season = days_lasted %>% str_replace("[A-Z]", "-"))
## # A tibble: 10 × 4
##    name                    reason_tapped_out              days_lasted season
##    <chr>                   <chr>                                <dbl> <chr> 
##  1 Tracy Wilson            Afraid of bears                          8 8     
##  2 Zachary Gault           Cut arm with axe                         8 8     
##  3 Kielyn Marrone          Starvation                              80 80    
##  4 Callie North            Felt like journey was complete          72 72    
##  5 Wayne Russell           Fear of bears                            4 4     
##  6 Brad Richardson         Had no food the whole time               7 7     
##  7 Jacques Turcotte        Missed his family                       15 15    
##  8 Callie Russell          Frostbite of the toes                   89 89    
##  9 Theresa Emmerich Kamper Low BMI, lost too much weight           69 69    
## 10 Tim Madsen              Anxiety attack, chest pains              6 6
data_small %>% mutate(season = days_lasted %>% str_replace_all("[A-Z]", "-"))
## # A tibble: 10 × 4
##    name                    reason_tapped_out              days_lasted season
##    <chr>                   <chr>                                <dbl> <chr> 
##  1 Tracy Wilson            Afraid of bears                          8 8     
##  2 Zachary Gault           Cut arm with axe                         8 8     
##  3 Kielyn Marrone          Starvation                              80 80    
##  4 Callie North            Felt like journey was complete          72 72    
##  5 Wayne Russell           Fear of bears                            4 4     
##  6 Brad Richardson         Had no food the whole time               7 7     
##  7 Jacques Turcotte        Missed his family                       15 15    
##  8 Callie Russell          Frostbite of the toes                   89 89    
##  9 Theresa Emmerich Kamper Low BMI, lost too much weight           69 69    
## 10 Tim Madsen              Anxiety attack, chest pains              6 6