Import your data

nuke <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2019/2019-08-20/nuclear_explosions.csv")
## Rows: 2051 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (6): country, region, source, purpose, name, type
## dbl (10): date_long, year, id_no, latitude, longitude, magnitude_body, magni...
## 
## ℹ 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.
nuke %>% glimpse()
## Rows: 2,051
## Columns: 16
## $ date_long         <dbl> 19450716, 19450805, 19450809, 19460630, 19460724, 19…
## $ year              <dbl> 1945, 1945, 1945, 1946, 1946, 1948, 1948, 1948, 1949…
## $ id_no             <dbl> 45001, 45002, 45003, 46001, 46002, 48001, 48002, 480…
## $ country           <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "US…
## $ region            <chr> "ALAMOGORDO", "HIROSHIMA", "NAGASAKI", "BIKINI", "BI…
## $ source            <chr> "DOE", "DOE", "DOE", "DOE", "DOE", "DOE", "DOE", "DO…
## $ latitude          <dbl> 32.54, 34.23, 32.45, 11.35, 11.35, 11.30, 11.30, 11.…
## $ longitude         <dbl> -105.57, 132.27, 129.52, 165.20, 165.20, 162.15, 162…
## $ magnitude_body    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ magnitude_surface <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ depth             <dbl> -0.100, -0.600, -0.600, -0.200, 0.030, -0.080, -0.08…
## $ yield_lower       <dbl> 21.0, 15.0, 21.0, 21.0, 21.0, 37.0, 49.0, 18.0, 22.0…
## $ yield_upper       <dbl> 21.0, 15.0, 21.0, 21.0, 21.0, 37.0, 49.0, 18.0, 22.0…
## $ purpose           <chr> "WR", "COMBAT", "COMBAT", "WE", "WE", "WR", "WR", "W…
## $ name              <chr> "TRINITY", "LITTLEBOY", "FATMAN", "ABLE", "BAKER", "…
## $ type              <chr> "TOWER", "AIRDROP", "AIRDROP", "AIRDROP", "UW", "TOW…

Chapter 14

Tools

Detect matches

nuke %>%
        summarise(sum(str_detect(country, "USA")))
## # A tibble: 1 × 1
##   `sum(str_detect(country, "USA"))`
##                               <int>
## 1                              1032
str_detect(nuke$country, "USA") %>% head(n = 10)
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
sum(str_detect(nuke$country, "USA"))
## [1] 1032
mean(str_detect(nuke$country, "USA"))
## [1] 0.5031692

Extract matches

countries <- c("USA", "USSR", "FRANCE", "UK", "CHINA", "INDIA", "PAKISTAN")
country_match <- str_c(countries, collapse = "|")
country_match
## [1] "USA|USSR|FRANCE|UK|CHINA|INDIA|PAKISTAN"
# Extract strings with a country
has_country <- str_subset(nuke$country, country_match)
str_extract(has_country, country_match) %>% head(n = 10)
##  [1] "USA"  "USA"  "USA"  "USA"  "USA"  "USA"  "USA"  "USA"  "USSR" "USA"

Replacing matches

nuke %>%
        mutate(country_rev = country %>% str_replace("^[A-Z]", "-")) %>%
        select(country, country_rev)
## # A tibble: 2,051 × 2
##    country country_rev
##    <chr>   <chr>      
##  1 USA     -SA        
##  2 USA     -SA        
##  3 USA     -SA        
##  4 USA     -SA        
##  5 USA     -SA        
##  6 USA     -SA        
##  7 USA     -SA        
##  8 USA     -SA        
##  9 USSR    -SSR       
## 10 USA     -SA        
## # ℹ 2,041 more rows
nuke %>%
        mutate(country_rev = country %>% str_replace_all("[A-Z]", "-")) %>%
        select(country, country_rev)
## # A tibble: 2,051 × 2
##    country country_rev
##    <chr>   <chr>      
##  1 USA     ---        
##  2 USA     ---        
##  3 USA     ---        
##  4 USA     ---        
##  5 USA     ---        
##  6 USA     ---        
##  7 USA     ---        
##  8 USA     ---        
##  9 USSR    ----       
## 10 USA     ---        
## # ℹ 2,041 more rows