Skip to content

Instantly share code, notes, and snippets.

@Houstonwp
Created May 29, 2019 20:12
Show Gist options
  • Save Houstonwp/a5aea265ac890a3605f6afa6d13a9667 to your computer and use it in GitHub Desktop.
Save Houstonwp/a5aea265ac890a3605f6afa6d13a9667 to your computer and use it in GitHub Desktop.
Get SOA Mortality Table Information
library(tidyverse)
library(xml2)
get_table_info <- function(.path) {
possibly_read_xml <- purrr::possibly(read_xml, otherwise = NULL)
.xml <- possibly_read_xml(.path)
if (!is.null(.xml)) {
table_info_columns <-
.xml %>%
xml_find_all("//ContentClassification") %>%
xml_children() %>%
xml_name()
table_info <-
.xml %>%
xml_find_all("//ContentClassification") %>%
xml_children() %>%
xml_text()
tibble(table_info_columns, table_info) %>%
group_by(table_info_columns) %>%
summarise(table_info = paste(table_info, collapse = ", ")) %>%
spread(key = table_info_columns, value = table_info) %>%
separate(KeyWord,
into = c("table_layout", "usage", "nation"),
sep = ", ")
}
}
urls <-
1076:1085 %>%
map_chr( ~ paste0("https://mort.soa.org/data/t", .x, ".xml"))
cso_info <-
urls %>%
map_dfr( ~ get_table_info(.x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment