Providing relocation assistance

Programming with dplyr

Dr. Chester Ismay

Educator, Data Scientist, and R/Python Consultant

The column names of world_bank_data

names(world_bank_data)
 [1] "iso"                   "country"               "continent"            
 [4] "region"                "year"                  "infant_mortality_rate"
 [7] "fertility_rate"        "perc_electric_access"  "perc_college_complete"
[10] "perc_cvd_crd_70"       "unemployment_rate"     "perc_rural_pop"
Programming with dplyr

Moving with select()

reordered_wb <- world_bank_data %>% 
    select(iso:year,

matches("^perc"),
everything())
names(reordered_wb)
 [1] "iso"                   "country"               "continent"           
 [4] "region"                "year"                  "perc_electric_access" 
 [7] "perc_college_complete" "perc_cvd_crd_70"       "perc_rural_pop"       
[10] "infant_mortality_rate" "fertility_rate"        "unemployment_rate"
Programming with dplyr

Using last_col() instead

world_bank_data %>% 
    select(iso:year, 
    matches("^perc"),

infant_mortality_rate:last_col()) %>%
names()
 [1] "iso"                   "country"               "continent"         
 [4] "region"                "year"                  "perc_electric_access" 
 [7] "perc_college_complete" "perc_cvd_crd_70"       "perc_rural_pop"       
[10] "infant_mortality_rate" "fertility_rate"        "unemployment_rate"
Programming with dplyr

A simpler way with relocate() and .after

world_bank_data %>% 

relocate(matches("^perc"), .after = year) %>%
names()
 [1] "iso"                   "country"               "continent"           
 [4] "region"                "year"                  "perc_electric_access" 
 [7] "perc_college_complete" "perc_cvd_crd_70"       "perc_rural_pop"       
[10] "infant_mortality_rate" "fertility_rate"        "unemployment_rate"
Programming with dplyr

relocate() with .before

world_bank_data %>%
  relocate(matches("^perc"), 
           .before = infant_mortality_rate) %>%

names()
 [1] "iso"                   "country"               "continent"           
 [4] "region"                "year"                  "perc_electric_access" 
 [7] "perc_college_complete" "perc_cvd_crd_70"       "perc_rural_pop"       
[10] "infant_mortality_rate" "fertility_rate"        "unemployment_rate"
Programming with dplyr

Let's practice!

Programming with dplyr

Preparing Video For Download...