Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Created February 23, 2021 16:28
Show Gist options
  • Save ikashnitsky/0f93062f2b67eeac69949554027fa84f to your computer and use it in GitHub Desktop.
Save ikashnitsky/0f93062f2b67eeac69949554027fa84f to your computer and use it in GitHub Desktop.
Handy functions to quickly load locally stored HMD files -- get the zip from https://www.mortality.org/cgi-bin/hmd/hmd_download.php
#===============================================================================
# 2021-02-23 -- local hmd
# Function to read in a whole local HMD folder
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
# a function to read ONE file
fread_hmd <- function(x) x %>%
data.table::fread(skip = 2, na.strings = ".") %>%
mutate(Age = Age %>% str_remove("\\+") %>% as.integer()) %>%
mutate_all(as.numeric)
# a function to read in the whole directory
fread_hmd_dir <- function(thedir) {
require(fs)
require(magrittr)
require(janitor)
suppressWarnings(
thedir %>%
dir_ls() %>%
map_df(fread_hmd, .id = "country") %>%
janitor::clean_names() %>%
mutate(
country = country %>%
str_remove(thedir %>% path_real()) %>%
str_remove("\\..*") %>%
str_remove("\\/"),
age = age %>% str_remove("\\+") %>% as.integer()
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment