How to Search for Census Data from R

November 16, 2015
By

(This article was first published on AriLamstein.com » R, and kindly contributed to R-bloggers)

In my course Learn to Map Census Data in R I provide people with a handful of interesting demographics to analyze. This is convenient for teaching, but people often want to search for other demographic statistics. To address that, today I will work through an example of starting with a simple demographic question and using R to answer it.

Here is my question: I used to live in Japan, and to this day I still enjoy practicing Japanese with native speakers. If I wanted to move from San Francisco to a part of the country that has more Japanese people, where should I move?

Step 1: Find the Table for the Data

Data in the census bureau is stored in tables. One way to find the table for a particular metric is to use the function ?acs.lookup from the acs package. (Note that to run this code you will need to get and install a census API key; I explain how to do that here).

> library(acs)

> acs.lookup(keyword = "Japanese", endyear = 2013)
An object of class "acs.lookup"
endyear= 2013  ; span= 5 

results:
  variable.code table.number                                                                    table.name                                   variable.name
1    B02006_009       B02006                                                Asian Alone By Selected Groups                                       Japanese 
2    B16001_069       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs                                      Japanese: 
3    B16001_070       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs            Japanese: Speak English 'very well' 
4    B16001_071       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs  Japanese: Speak English less than 'very well' 

The Census Bureau has two “Japanese” tables: the first relates to race and the second to language. For simplicity, let’s focus on race (B02006). The “_009” at the end indicates the column of the table; each column tabulates a different Asian nationality. 

Step 2: Get the Data

There are a few ways to get the data from that table into R. One way is to use the function ?acs.fetch in the acs package. If your end result is to map the data with the choroplethr package, however, you might find it easier to use the function ?get_acs_data in the choroplethr package:

> library(choroplethr)

> l = get_acs_data("B02006", "county", column_idx=9)

What’s returned is a list with 2 elements. The first element is a data frame with the (region, value) pairs. The second element is the title of the column:

str(l)
List of 2
 $ df :'data.frame': 3143 obs. of 2 variables:
 ..$ region: num [1:3143] 1001 1003 1005 1007 1009 ...
 ..$ value : num [1:3143] 10 25 0 0 0 0 0 103 2 19 ...
 $ title: chr "Asian Alone By Selected Groups: Japanese"

Step 3: Analyze the Data

The first way to analyze the data is to simply look at the data frame:

> df = l[[1]]

> head(df)
region value
1 1001 10
2 1003 25
3 1005 0
4 1007 0
5 1009 0
6 1011 0

People who have taken my course will recognize the regions as FIPS County Codes. We can use a boxplot to look at the distribution of values:

boxplot(df$value)

boxplot-japanese-residents

I draw two conclusions from this chart: 1) the median is very low and 2) there are two very large outliers.

To find out the names of the outliers we need to convert the FIPS Codes to English. We can do that by merging df with the data frame ?county.regions.

> data(county.regions)
> head(county.regions)
   region county.fips.character county.name state.name state.fips.character state.abb
1    1001                 01001     autauga    alabama                   01        AL
36   1003                 01003     baldwin    alabama                   01        AL
55   1005                 01005     barbour    alabama                   01        AL
15   1007                 01007        bibb    alabama                   01        AL
2    1009                 01009      blount    alabama                   01        AL
16   1011                 01011     bullock    alabama                   01        AL

> df2 = merge(df, county.regions)
> df2 = df2[order(-df2$value), ]
> head(df2)
     region  value county.fips.character county.name state.name state.fips.character state.abb
548   15003 150984                 15003    honolulu     hawaii                   15        HI
205    6037 103180                 06037 los angeles california                   06        CA
216    6059  33211                 06059      orange california                   06        CA
229    6085  28144                 06085 santa clara california                   06        CA
2971  53033  21493                 53033        king washington                   53        WA
223    6073  18592                 06073   san diego california                   06        CA

So the outliers are Honolulu county and Los Angeles county. San Francisco isn’t even in the top 6. So if I ever decide to give up my career in technology for a career focused on Japanese, I should move to Honolulu!

It’s also easy to create a choropleth map of the values. This allows us to see the geographic distribution of the values.

library(choroplethrMaps)

county_choropleth(df, title = "2012 County Estimates:nNumber of Japanese per County")

japanese-map

According to this map, by living on the west coast I am already in a part of the country with a high concentration of Japanese people.

Conclusion

If you wind up using this blog post to do an analysis of your own, or have difficulty adapting this code to your own purposes, please leave a comment below. I’m always interested in hearing what my readers are working on.

A final note to my Japanese friends: どう思いますか?アメリカで一番興味がある場所はホノルルとロサンゼルスですか?口コミしてください!

The post How to Search for Census Data from R appeared first on AriLamstein.com.

To leave a comment for the author, please follow the link and comment on their blog: AriLamstein.com » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)