Last active
April 11, 2025 09:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #=============================================================================== | |
| # 2021-02-23 -- local hmd UPD 2025-04-11 | |
| # 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(tidyverse) | |
| require(janitor) | |
| suppressWarnings( | |
| thedir %>% | |
| dir_ls() %>% | |
| map_df(fread_hmd, .id = "country") %>% | |
| janitor::clean_names() %>% | |
| mutate( | |
| country = country %>% | |
| # remove everything in the string before the last forward slash | |
| str_remove("^.*/") |> | |
| # remove everything after the first dot | |
| 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