Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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