Fetching Census Data from American Community Survey (ACS)

Using API is a great of retrieving information especially throught the internet of overwhelming information. Today, I demonstrate a way to retrieve information from the American Comminuty Survey using an iterative approach. The reason for this design is because the API have changed over the year and it is no longer possible to retrieve data for all Census Tract for a large unit of geography. In this case, I would like to retrieve data for Michigan State.

There are two major parts of R Coding. The first is the function takes in the search parameter which is the table name and the geography area. The second is the function caller which accepts data from the ACS API and further process the data. For proprietary reasons I cannot disclose the calculations the snippets below would allow retrieving a large dataset of large geographies. This information is useful for doing geospatial analysis, resulting in reports that helps policy makers, developers and other stakeholders to make better decision based on data of the American population.

Retrieval Mechanism

counties <- geo.lookup(state="MI",county = "*")[4]
counties <- counties[!is.na(counties)]

acs.fetcher <- function(acsvar,year=acsyear) {
  for (county in counties) {
    acs.obj.itr <- NULL
    while( is.null(acs.obj.itr) && attempt <= 3 ) {
      attempt <- attempt + 1
        acs.obj.itr <- acs.fetch(geography=geo.make(state = "IL",county=county, tract = "*"),
                                 endyear=year,variable = acsvar)
    # if(!exists("est") && !exists("se")){
      est <- estimate(acs.obj.itr)
      se <- standard.error(acs.obj.itr)
    else {
      est <- rbind(est,estimate(acs.obj.itr))
      se <- rbind(se,standard.error(acs.obj.itr))
    # print(head(estimate(acs.obj.itr),1))
    # if (i>=3){break()}
  acs.obj.itr@estimate <- est
  acs.obj.itr@standard.error <- se

Retriever and Information Processing

acsyear = 2017
tblnam <- c("Population_by_Age","...")
tblnam <- paste0("ACS",acsyear,"_",tblnam)

tname <- grep("B01001", tblnam, value=TRUE)[1]
age.var <- acs.lookup(table.number = "B01001",span = 5,endyear = 2015, dataset = "acs") # create variable
age <- acs.fetcher(age.var,acsyear)