Skip to content

Instantly share code, notes, and snippets.

@sysilviakim
Last active June 11, 2019 18:19
Show Gist options
  • Select an option

  • Save sysilviakim/2552453786ecd03a3ef0547f3e3ea7aa to your computer and use it in GitHub Desktop.

Select an option

Save sysilviakim/2552453786ecd03a3ef0547f3e3ea7aa to your computer and use it in GitHub Desktop.
Pre-download a census object to be used in wru::wru
library(tidyverse)
library(magrittr)
if (!require("wru")) {
devtools::install_github("sysilviakim/wru")
}
library(wru)
census_2010_age_sex <- census_2010_sex <- census_2010_age <- census_2010 <-
c(state.abb, "DC") %>% set_names(., .) %>% map(~list())
key <- "CENSUS_KEY"
## No age, No sex
for (geo_unit in c("county", "tract", "block")) {
for (sta in sort(c(state.abb, "DC"))) {
if (is.null(census_2010[[sta]][[geo_unit]])) {
census_2010[[sta]][[geo_unit]] <- census_geo_api(
key = key, state = sta, geo = geo_unit, age = FALSE, sex = FALSE,
retry = 1e22, save_temp = paste0(tolower(sta), "_temp.Rda")
)
gc(reset = TRUE)
save(census_2010, file = "census_2010.Rda")
}
}
}
## Age, no sex
for (geo_unit in c("county", "tract", "block")) {
for (sta in sort(c(state.abb, "DC"))) {
if (is.null(census_2010[[sta]][[geo_unit]])) {
census_2010_age[[sta]][[geo_unit]] <- census_geo_api(
key = key, state = sta, geo = geo_unit, age = TRUE, sex = FALSE,
retry = 1e22, save_temp = paste0(tolower(sta), "_temp.Rda")
)
gc(reset = TRUE)
save(census_2010_age, file = "census_2010_age.Rda")
}
}
}
## Sex, no age
for (geo_unit in c("county", "tract", "block")) {
for (sta in sort(c(state.abb, "DC"))) {
if (is.null(census_2010[[sta]][[geo_unit]])) {
census_2010_sex[[sta]][[geo_unit]] <- census_geo_api(
key = key, state = sta, geo = geo_unit, age = FALSE, sex = TRUE,
retry = 1e22, save_temp = paste0(tolower(sta), "_temp.Rda")
)
gc(reset = TRUE)
save(census_2010_sex, file = "census_2010_sex.Rda")
}
}
}
## Both age and sex
for (geo_unit in c("county", "tract", "block")) {
for (sta in sort(c(state.abb, "DC"))) {
if (is.null(census_2010[[sta]][[geo_unit]])) {
census_2010_age_sex[[sta]][[geo_unit]] <- census_geo_api(
key = key, state = sta, geo = geo_unit, age = TRUE, sex = TRUE,
retry = 1e22, save_temp = paste0(tolower(sta), "_temp.Rda")
)
gc(reset = TRUE)
save(census_2010_age_sex, file = "census_2010_age_sex.Rda")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment