Watts-College / cpp-529-spr-2022

https://watts-college.github.io/cpp-529-spr-2022/
0 stars 2 forks source link

Step 4: Transforming SF to SP for Cartogram #12

Closed droach7 closed 2 years ago

droach7 commented 2 years ago

@u12345 , @JasonSills

For Step 4 I keep getting this error message, Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': subscript out of bounds , after running this line of code msp.sp <- as_Spatial( msp ).

I have selected Sacramento, CA as my MSA to explore. As this does not cross multiple states, I only needed one Census API call and therefore didn't need to bind map data from each state into 'msp.pop'. All my other data up to that point has been fine and is included below in one chunk for ease of viewing, however in my lab it is chunked out.

crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv",  stringsAsFactors=F, colClasses="character" )

# search for cities' names by strings, use the ^ anchor for "begins with" 
grep( "^SAC", crosswalk$msaname, value=TRUE ) 
these.msp <- crosswalk$msaname == "SACRAMENTO, CA" 
these.fips <- crosswalk$fipscounty[ these.msp ] 
these.fips <- na.omit( these.fips ) # omit NAs

head(these.fips) %>% pander() # print the full FIPS (state & county)

state.fips <- substr( these.fips, 1, 2 ) # extract the state FIPs (06)
county.fips <- substr( these.fips, 3, 5 ) # extract the county FIPs (017, 061, 067)

cbind( these.fips, state.fips, county.fips ) %>% pander()

# use "06" as state / state.fips as determined above
# only 1 census API call since SAC MSA is only in CA
msp.pop <- 
get_acs( geography = "tract", variables = "B01003_001",
         state = "06", county = county.fips[state.fips=="06"], geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds"
census.dat <- readRDS(gzcon(url( URL )))

# can merge an sf object and data.frame
msp <- merge( msp.pop, census.dat, by.x="GEOID", by.y="tractid" )

# make sure there are no empty polygons
msp <- msp[ ! st_is_empty( msp ) , ]

Can you please help me figure out why I keep getting this error code?

JasonSills commented 2 years ago

Yes, I believe this has to do with the state code for the city you chose. You have a state that starts with a "06". Anytime you are having an issue with your data or tool you're using for analysis a number (here it is R, but this comes up anywhere) starting with a "0" is a red flag. You need to make the data numeric.

But before we move on I also want to point out the use of "msp". This is the used in the dataset for the location Minneapolis-St Paul. For your location I recommend changing this default to something that makes sense for your specific code.

To try to fix this error let's use the following code: msp.pop$GEOID <- as.numeric(msp.pop$GEOID)

and place it here: can merge an sf object and data.frame msp.pop$GEOID <- as.numeric(msp.pop$GEOID) msp <- merge( msp.pop, census.dat, by.x="GEOID", by.y="tractid" )

Now run it and see if it works.

droach7 commented 2 years ago

@JasonSills

Sweet! Yes, that worked. I knew naming variables beginning with a number was problematic, but I guess I hadn't realized that using any numerical data starting with a 0 was problematic. Thanks for the help!

And yes, I know msp was used in the example for Minneapolis-St. Paul. I typically like to use the same names for variables while I run through the code and try to replicate it for my own chosen city, that way I know it is less likely to be a spelling mistake. Then in the end I go back and change the variable name to match.

droach7 commented 2 years ago

resolved