Import your data

data <- read_csv("../00_data/myData.csv")
## Rows: 20755 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Entity, Code
## dbl (2): Year, LifeExpectancy
## 
## ℹ 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

data %>% 
    summarise(sum(str_detect(Year,"1$")))
## # A tibble: 1 × 1
##   `sum(str_detect(Year, "1$"))`
##                           <int>
## 1                          2276

Extract matches

data3 <- c("1950", "1951","1952")

Replacing matches

data %>% mutate(Entity = Entity %>% str_replace("[A-Z]", "-"))
## # A tibble: 20,755 × 4
##    Entity      Code   Year LifeExpectancy
##    <chr>       <chr> <dbl>          <dbl>
##  1 -fghanistan AFG    1950           27.7
##  2 -fghanistan AFG    1951           28.0
##  3 -fghanistan AFG    1952           28.4
##  4 -fghanistan AFG    1953           28.9
##  5 -fghanistan AFG    1954           29.2
##  6 -fghanistan AFG    1955           29.9
##  7 -fghanistan AFG    1956           30.4
##  8 -fghanistan AFG    1957           30.9
##  9 -fghanistan AFG    1958           31.5
## 10 -fghanistan AFG    1959           32.0
## # ℹ 20,745 more rows