Skip to content

Instantly share code, notes, and snippets.

@szimmer
Created September 27, 2019 12:52
Show Gist options
  • Save szimmer/564a77ecb4cc5840b39c36d6965fad71 to your computer and use it in GitHub Desktop.
Save szimmer/564a77ecb4cc5840b39c36d6965fad71 to your computer and use it in GitHub Desktop.
Getting tract level population estimates for multiple years
library(tidycensus)
library(tidyverse)
#census_api_key("YOUR API KEY GOES HERE")
#Get an API key by going here: https://api.census.gov/data/key_signup.html
# First this tells you what variables are on the ACS
v17 <- load_variables(2017, "acs1")
# You can only get tract estimates within states
# You can only get one year of data at a time
# First setting up vectors of states and years
# Assuming we want to include DC
# Example is for getting data from 2010 to 2017, earlier than that is not possible through this method
# Note that these are 5 year estimates. Getting tract level estimates isn't possible for 1-year data
StateList <- c(state.abb, "DC")
YearList <- 2010:2017
StateCombo <- rep(StateList, each=length(YearList)) # We need each state replicated by the number of years
YearCombo <- rep(YearList, length(StateList)) # We need each year replicated by the number of states
PopEstsYearState <- function(Year, State){
get_acs("tract", variables="B01001_001", year=Year, state=State, survey="acs5") %>%
mutate(Year=Year)
}
AllTractsAllYears <-
map2_df(YearCombo, StateCombo, PopEstsYearState) %>%
rename(Population=estimate, MOE_Population=moe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment