# rebuild a clean GEOID (I was getting mostly NA values for some reason for GEOID)tx_census_tracts <- tx_census_tracts %>%mutate(GEOID_clean =paste0(STATEFP, COUNTYFP, TRACTCE) )# checkhead(tx_census_tracts$GEOID_clean)
# double check#unique(census_tracts$GEOID_clean)# inspect new GEOIDs in Exceltract_ids <-unique(tx_census_tracts$GEOID_clean)write.csv(tract_ids, "All_Census_Tract_GEOIDs.csv", row.names =FALSE)# there are 6,896 tracts in Texas according to this list
# make sure both layers have valid geometryaustin_jur_filtered <-st_make_valid(austin_jur_filtered)tx_census_tracts <-st_make_valid(tx_census_tracts)
# quick check that the selected tracts look reasonableprint(paste("# of tracts intersecting Austin:", nrow(selected_tracts)))
[1] "# of tracts intersecting Austin: 383"
print(paste("# of unique GEOIDs:", length(unique(selected_tracts$GEOID_clean))))
[1] "# of unique GEOIDs: 271"
There are 271 intersecting census tracts, meaning there are 271 tracts in Austin full and limited purpose jurisdictions. This makes sense, as Travis County alone has 291 census tracts, though not all of Travis County is in Austin. There are a total of 493 census tracts in all of Bastrop, Hays, Travis, and Williamson Counties combined.
The reason there are 383 rows, but only 271 distinct tracts, could be because some tracts may touch multiple Austin jurisdiction polygons since Austin’s boundary is split into parts.
# quick view of unique GEOIDsprint(sort(unique(selected_tracts$GEOID_clean))[1:20])
# to save only distinct GEOIDs with no duplicatesselected_unique <- selected_tracts %>%distinct(GEOID_clean, .keep_all =TRUE)
Save Final Files
# save the intersected tracts shapefilest_write(selected_tracts, "Austin_LTD_FULL_Tracts.shp")
Warning in abbreviate_shapefile_names(obj): Field names abbreviated for ESRI
Shapefile driver
Writing layer `Austin_LTD_FULL_Tracts' to data source
`Austin_LTD_FULL_Tracts.shp' using driver `ESRI Shapefile'
Writing 383 features with 24 fields and geometry type Polygon.
# Also export a CSV list of tract GEOIDs and namestract_list <- selected_tracts %>%st_drop_geometry() %>%arrange(GEOID_clean)write_csv(tract_list, "Austin_LTD_FULL_Tract_List.csv")#################################################################################### files without duplicate GEOIDsst_write(selected_unique, "Austin_LTD_FULL_Tracts_Unique.shp")
Warning in abbreviate_shapefile_names(obj): Field names abbreviated for ESRI
Shapefile driver
Writing layer `Austin_LTD_FULL_Tracts_Unique' to data source
`Austin_LTD_FULL_Tracts_Unique.shp' using driver `ESRI Shapefile'
Writing 271 features with 24 fields and geometry type Polygon.
Checking to see if I still get 271 tracts when I join my shapefile to Coda’s shapefile. Coda’s shapefile contains all census tracts in Bastrop, Hays, Travis, and Williamson counties.
# check which of my 271 unique GEOIDs are in Coda's filecommon_geo <-intersect(unique(selected_unique$GEOID_clean),unique(CRG_tracts$GEOID_clean))setdiff(unique(selected_unique$GEOID_clean), unique(CRG_tracts$GEOID_clean)) # not in CRG's file
character(0)
length(CRG_tracts$GEOID_clean)
[1] 492
# how many unique GEOID's in Coda's filelength(unique(CRG_tracts$GEOID_clean))