[acs-r] acs-r Digest, Vol 13, Issue 1

Ezra Haber Glenn eglenn at mit.edu
Tue Oct 14 15:29:16 EDT 2014


Sorry -- there is a slight difference related to whether you are
working run the "apply" command to sum the rows of the acs object
(which has estimate and errors) and then extract the estimates (which
is what I did) or extract the estimates and then run the apply command
to sum them (which is what you did).

Once you extract the estimates, you are working with a object of class
"matrix," not an acs-class object.  This creates two issues:

1) Unlike dataframes and acs objects and lots of other R data objects,
matrices don't allow you to just add a new column by assigning
something to a part of the data that isn't there yet.  So is my.data
is dataframe or acs object with three columns and I say
my.data[,4]=XXXXXX, R doesn't worry about the fact that there isn't
anything in my.data[,4] yet -- it just adds the new data to there.
But for a matrix, this breaks some of the rules -- you need to create
a new matrix that combines the old matrix with the new data, maybe
using cbind or something like that.

Even worse, it turns out that matrices are a little backwards
dimension-wise, which confuses the script I gave you: in the apply
command, do you see where is says "MARGIN=2"?  That tells is you want
it to sum across the rows (the second dimension of the object), not
the columns (which would be the first dimension -- MARGIN=1).  When
you extract the estimates and thus make a matrix, these dimensions are
reversed -- the rows are the "first" dimension and the columns the
second.  (This has to do with the way R deals with matrices -- unlike
dataframes and acs objects, which have an obvious two-dimensional
spreadsheet like structure, matrices can have any number of
dimensions, and I guess rows are first and columns are second.)

So, either of the following would work:

> library(acs)
> us.county=geo.make(state="LA", county="*")
> us.fb= acs.fetch(geography=us.county, table.number="B05006")
> my.data.est=estimate(us.fb[,111:113])
> my.data.est=cbind(my.data.est, apply(X=my.data.est, FUN=sum, MARGIN=1))


> library(acs)
> us.county=geo.make(state="LA", county="*")
> us.fb= acs.fetch(geography=us.county, table.number="B05006")
> my.data=us.fb[,111:113]
> my.data[,4]=apply(X=my.data, FUN=sum, MARGIN=2)
> estimate(my.data)

Sorry for the added confusion.  My advice: work with the data in the
acs format (the second option here), and only extract the estimates
when you want them. 

--Ezra

At Tue, 14 Oct 2014 14:54:09 -0400, Nevin Krishna wrote:
> 
> Hi Ezra,
>  
> I implemented some of the code you supplied but ran into a problem with the portion that
> deals with creating  a new variable to catch the sum of all three variables. Here is the
> code I used:
>  
> library(acs)
> us.county=geo.make(state="LA", county="*")
>   us.fb= acs.fetch(geography=us.county, table.number="B05006")
>           my.data.est=estimate(us.fb[,111:113])
>           my.data.est[,4]=apply(X=my.data.est, FUN=sum, MARGIN=2)
>  
> when I run  my.data.est, I get the following  error:
> 
> Error in `[<-`(`*tmp*`, , 4, value = c(155, 1241, 223)) : 
>   subscript out of bounds
> 
> I am looking to get an output that looks like this:
>                                                     B05006_111 B05006_112 B05006_113 
> combined
> Acadia Parish, Louisiana                        0                 0            
>  0                          0
> Allen Parish, Louisiana                           0                 25         
>  10                       35
> Ascension Parish, Louisiana                  0                  0            
>  0                         0
> 
> Any ideas on how to correct this?
>  
> Nevin
> 
> 

--
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