Open kirstenronning opened 3 years ago
Congratulations, you have discovered the leading zeros problem.
If you audit your data trail you will see that you have 605 tracts in Denver, but they disappear after the merge step.
The problem is your state FIPS is 08, which has a leading zero. Compare your GEOID and tractid here - see the difference?
(08001009326 vs 8001009326)
> head( denver.pop )
Simple feature collection with 6 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -105.035 ymin: 39.56598 xmax: -104.9042 ymax: 39.89966
geographic CRS: NAD83
GEOID POP geometry
1 08001009326 2721 MULTIPOLYGON (((-105.025 39...
2 08001009411 3054 MULTIPOLYGON (((-105.0345 3...
3 08005005553 4098 MULTIPOLYGON (((-105.0348 3...
4 08005005627 4733 MULTIPOLYGON (((-104.9602 3...
5 08005006100 3184 MULTIPOLYGON (((-104.9878 3...
6 08005006708 5151 MULTIPOLYGON (((-104.923 39...
> head( census.dat )
tractid statea countya tracta pnhwht12 pnhblk12 phisp12 pntv12 pasian12 phaw12 pindia12 pchina12 pfilip12
1 1001020100 01 001 020100 85.32 11.53 0.00 0.17 0.00 0 0.00 0.00 0.00
2 1001020200 01 001 020200 37.02 56.27 2.52 0.00 2.84 0 0.00 0.00 0.41
3 1001020300 01 001 020300 79.78 17.15 1.77 0.00 1.08 0 0.00 0.00 0.00
4 1001020400 01 001 020400 92.60 1.45 2.63 0.83 0.00 0 0.00 0.00 0.00
5 1001020500 01 001 020500 75.30 18.10 2.53 0.18 2.41 0 0.32 0.97 0.10
6 1001020600 01 001 020600 77.45 9.60 10.23 0.00 0.00 0 0.00 0.00 0.00
The fix is to convert both to numeric strings.
denver.pop$GEOID <- as.numeric( denver.pop$GEOID )
Then should work:
> denver.pop <-
+ get_acs( geography = "tract",
+ variables = "B01003_001",
+ state = "08",
+ county = county.fips,
+ geometry = TRUE ) %>%
+ select( GEOID, estimate ) %>%
+ rename( POP=estimate )
Getting data from the 2014-2018 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
>
> nrow( denver.pop )
[1] 605
> denver <- merge( denver.pop, census.dat, by.x="GEOID", by.y="tractid" )
> nrow( denver )
[1] 0
>
> # can merge an sf object and data.frame
> denver.pop$GEOID <- as.numeric( denver.pop$GEOID )
> denver <- merge( denver.pop, census.dat, by.x="GEOID", by.y="tractid" )
> nrow( denver )
[1] 605
@lecy thank you for helping me with this!
No problem!
Hello, I seem to be having issues with downloading the correct shape file for my metropolitan area. I chose Denver, CO (or Denver-Aurora-Lakewood, CO), and I believe that I found the correct FIPS code.
With that being said, when I get to Step 4, I get an error message when I try to run
msp.sp <- as_Spatial( msp )
. It says "Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': subscript out of bounds."Furthermore, when I do get a shape file to pop up, it looks identical to the Minneapolis one provided in the example, leading me to believe that I am not doing this correctly. Am I supposed to put more information about Denver into the state.fips and county.fips lines?