acs.R Question: using FIPS codes as rownames

Posted by Ezra Glenn on October 28, 2014
Uncategorized

Q: An acs.R user asks:

Is it possible for an acs object to use FIPS codes for rownames?

A: Absolutely. Here’s how:

Start with some data:

> some.geo=geo.make(state=25, county=001, tract="*")
> some.data=acs.fetch(geography=some.geo, table.number="B01003")

Check out the geography functions:

> head(geography(some.data))
> 

The output of the final command should display the start of a dataframe with descriptive titles in the first column, but then fips codes for the state, county, and tract. When displaying acs objects, the first column of the object’s geography() dataframe is automatically used to name the rows. But this can be changed – see ?geography.

To use FIPS codes instead, we can extract the relevant columns from the object’s geography() and paste them together to recreate fully qualified FIPS codes. (The relevant columns are everything expect the first one, so “geography(some.data)[-1]” will do the trick.)

> my.fips.codes=apply(X=geography(some.data)[-1], MARGIN=1, FUN=paste, collapse="")
> 

Then we can re-assign the object’s geography() to include these codes as the first column:

> geography(some.data)=cbind(my.fips.codes, geography(some.data))
> 

Now try:

> head(some.data)
ACS DATA: 
 2007 -- 2011 ;
  Estimates w/90% confidence intervals;
  for different intervals, see confint()
          B01003_001  
251010100 2994 +/- 13 
251010206 2858 +/- 256
251010208 1903 +/- 260
251010304 2395 +/- 269
251010306 2616 +/- 270
251010400 3056 +/- 296
>  

and you should see FIPS codes as the rownames.

Important note: Given that I actually don’t work with FIPS codes all that often, there is a chance I’ve deviated slightly from the proper formatting here – you may need to paste in extra leading zeroes or something to make sure the pieces line up in that apply/paste command – but hopefully you get the idea. (For example, I think tract IDs are supposed to be six digits long, not three.)

Comments are closed.