bmc
bmc

Reputation: 857

r - query the census api and read in demographic variables for each zip code in Chicago

The Problem

I am trying to get some data from the cencus API by demographic variables for each zip code in Chicago. I want to pull down several variables, like age, income, ect..

What I've Tried

There is little to no examples with the acs package which was built for this. This is the best I've got so far ...

install.packages("acs")
library(acs)
acs.fetch(endyear, span = 5, geography, table.name,
table.number, variable, keyword, dataset = "acs",
key, col.names = "auto", ...)
# I have not really even sure how to access the functions inside the acs package at this point either ...

From this source on page 6 of the pdf. I am not sure how exactly I would use this function to pull exactly what I want, and I've tried several combinations of table.name=c("sex","age","education") and others, along with key=c("chicago","zipcode"), ect...

Any suggestions of how to use this function or use the acs package in R?

Upvotes: 0

Views: 746

Answers (2)

GL_Li
GL_Li

Reputation: 1798

Here is how to use package totalcensus to get data by zipcode. You can find this package from https://github.com/GL-Li/totalcensus.

library(totalcensus)
library(dplyr)
# http://www.city-data.com/zipmaps/Chicago-Illinois.html to find a list of zip
# code for Chicago
zips <- c(60007, 60018, 60068, 60106, 60131, 60176, 60601, 60602, 60603, 60604, 
          60605, 60606, 60607, 60608, 60609, 60610, 60611, 60612, 60613, 60614, 
          60615, 60616, 60617, 60618, 60619, 60620, 60621, 60622, 60623, 60624, 
          60625, 60626, 60628, 60629, 60630, 60631, 60632, 60633, 60634, 60636, 
          60637, 60638, 60639, 60640, 60641, 60642, 60643, 60644, 60645, 60646, 
          60647, 60649, 60651, 60652, 60653, 60654, 60655, 60656, 60657, 60659, 
          60660, 60661, 60706, 60707, 60714, 60804, 60827)

# read for all US zip codes from 2016 ACS 5-year survey
all_zip_acs5 <- read_acs5year(
    year = 2016,
    states = "US",
    table_contents = "below_poverty = B06012_006", # random example
    geo_headers = "ZCTA5",
    summary_level = "860"
)

# keep only Chicago zip codes
chicago_zip_acs5 <- filter(all_zip_acs5, ZCTA5 %in% zips)

    #           GEOID       lon      lat ZCTA5 state population below_poverty GEOCOMP SUMLEV        NAME
    # 1  86000US60007 -87.99736 42.00865 60007  <NA>      33733          1064     all    860 ZCTA5 60007
    # 2  86000US60018 -87.91176 41.97939 60018  <NA>      30519          2123     all    860 ZCTA5 60018
    # 3  86000US60068 -87.84343 42.01176 60068  <NA>      37567          1107     all    860 ZCTA5 60068
    # 4  86000US60106 -87.94183 41.95970 60106  <NA>      20215          1147     all    860 ZCTA5 60106
    # 5  86000US60131 -87.88426 41.93876 60131  <NA>      18072           943     all    860 ZCTA5 60131

Upvotes: 0

sconfluentus
sconfluentus

Reputation: 4993

You need to do a few things to get where you want to go.

First you need to register with ACS and get a key, install the key in your script and be sure to activate it or NOTHING will happen!

Then you need to create variables to drop into your requests:

zipCodes= c(00000,00000, 00000) #use as many as you have here
endYear<-2015

chicagoZipGeo <- geo.make(zip.code=zipCodes)

#Then you go fetch your data, and you need to use the appropriate codes to do so

pov.data <- acs.fetch(endyear = 2015, geography = chicagoZipGeo, 
                    table.number="B06012_002")

Notice that I used a weird capital A in the variable state, I did so for two reasons,

  1. so that you could see where it ends up in the geo.make part
  2. so that it does not interfere with the functions parameter of state to which you will feed your variable

You can look up the codes for things in this resource: 2015 5-Year ACS Code List

There are additional documents on the ACS site which tell you how to use the codes, the universe from which the estimates are drawn and such.

Upvotes: 0

Related Questions