[acs-r] Selecting

Ezra Haber Glenn eglenn at mit.edu
Tue Oct 14 11:07:00 EDT 2014


Nevin:

This is actually a lot easier than you might think -- you've pretty
much already got everything you need.  

The following will load the acs package, create the LA tracts
geography, and then fetch the data:

> library(acs)
> us.county=geo.make(state="LA", county="*")
> us.fb= acs.fetch(geography=us.county, table.number="B05006")

   Note that technically, you are fetching a lot more data than you
   even need here -- if you really only want three columns, you could
   just specify that with:

   > acs.fetch(geography=us.county, variable=c("B05006_111", "B05006_112", "B05006_113")

   (But for now, let's assume you downloaded the whole table into us.fb)

Now, how to get just a few columns out, and maybe sum them as well?

If you want to look at the data, you can just use:

> us.fb[,111:113]   # that's all the rows, columns 111 through 113

If you just want the estimates without the errors, try:

> estimate(us.fb[,111:113])


You could make a new acs object with just these three columns:

> my.data=us.fb[,111:113]

And then add a fourth column for the sum:

> my.data[,4]=apply(X=my.data, FUN=sum, MARGIN=2)

   Note that you could have done a few of these steps together, with 

   > my.data=cbind(us.fb[,111:113], apply(X=us.fb[,111:113], FUN=sum, MARGIN=2)) 

   But it amounts to the same thing either way.


You might want to give it nicer column names, such as:

> acs.colnames(my.data)=c("Liberia","Nigeria","Sierra Leone","Combined")


And then, as before, if you only want the estimates, you can just say:

> estimate(my.data)


One final warning: please note that the estimate() function allows you
to ignore the margins of error for this data, but this is not
recommended.  Especially when working with small parishes, the errors
may be larger than the perceived differences in the estimates -- but
this is a statistical issue, not a programming one.  In any event, let
the dowloader beware!

--Ezra  
 
At Mon, 13 Oct 2014 21:27:10 -0400, Nevin Krishna wrote:
> 
> Hello all,
> 
> i am fairly new to R and just started using the ACS package.
> 
> I have been playing with the examples in the link here:
> 
> http://eglenn.scripts.mit.edu/citystate/category/code/
>  
> I have adapted the code to look at another table (foreign born individuals) by county  in
> Louisiana:  5 year ACS ending in 2011: B05006
> In the example the code ultimately calculates a proportion which is then plotted on a map.
> I however want to create a dataset with the  estimate of the populations (no standard
> errors) of a few countries of origin (Liberia :HD01_VD111 and Nigeria: HD01_VD112 and
> Sierra Leone: HD01_VD112). also i would like to create a new variable say "combined" which
> is the sum of each of these variables.
> I cannot seem to figure out how to create a dataset with these variables. i am unsure of
> how to manipulate the code:
> 
> us.pub.fb=divide.acs(numerator=us.fb[,111], 
>                  denominator=us.fb[,1], method="proportion")
> pub.fb.est=data.frame(county=geography(us.pub.fb)[[1]], 
> 
> to not calculate a proportion but to simply list the variables of interst in a dataframe.
> below is the full code:
> 
> any suggestions on how to accomplish this would be appreciated.
> 
> Thanks in advance for your help!
> Nevin
> 
> PS..the full code below calculates the proportion:
> 
> us.county=geo.make(state="LA", county="*")
> 
> us.fb= acs.fetch(geography=us.county, table.number="B05006")
> 
> us.pub.fb=divide.acs(numerator=us.fb[,111], 
>                         denominator=us.fb[,1], method="proportion")
> pub.fb.est=data.frame(county=geography(us.pub.fb)[[1]], 
>                          percent.pub.trans=as.numeric(estimate(us.pub.fb)))
> # this next step is all for Louisiana!
> pub.fb.est$county=gsub("Parish", "County", pub.fb.est$county)
> # clean up county names and find the states
> pub.fb.est$state=gsub("^.*County, ", "", pub.fb.est$county)
> pub.fb.est$county=gsub(" County,.*", "", pub.fb.est$county)
> 
> 

--
Ezra Haber Glenn, AICP
Department of Urban Studies and Planning
Massachusetts Institute of Technology
77 Massachusetts Ave., Room 7-337
Cambridge, MA 02139
eglenn at mit.edu 
http://dusp.mit.edu/faculty/ezra-glenn | http://eglenn.scripts.mit.edu/citystate/
617.253.2024 (w)
617.721.7131 (c)


More information about the acs-r mailing list