Retrieve Census tract from Coordinates [closed]

只谈情不闲聊 提交于 2019-12-06 06:09:49

问题


I have a dataset with longitude and latitude coordinates. I want to retrieve the corresponding census tract. Is there a dataset or api that would allow me to do this?

My dataset looks like this:

       lat       lon   
1 40.61847 -74.02123   
2 40.71348 -73.96551   
3 40.69948 -73.96104    
4 40.70377 -73.93116   
5 40.67859 -73.99049   
6 40.71234 -73.92416   

I want to add a column with the corresponding census tract.

Final output should look something like this (these are not the right numbers, just an example).

       lat       lon     Census_Tract_Label   
1 40.61847 -74.02123                   5.01
2 40.71348 -73.96551                     20
3 40.69948 -73.96104                     41
4 40.70377 -73.93116                  52.02
5 40.67859 -73.99049                     58
6 40.71234 -73.92416                     60

回答1:


The tigris package includes a function called call_geolocator_latlon that should do what you're looking for. Here is some code using

    > coord <- data.frame(lat = c(40.61847, 40.71348, 40.69948, 40.70377, 40.67859, 40.71234),
    +                     long = c(-74.02123, -73.96551, -73.96104, -73.93116, -73.99049, -73.92416))
    > 
    > coord$census_code <- apply(coord, 1, function(row) call_geolocator_latlon(row['lat'], row['long']))
    > coord
           lat      long     census_code
    1 40.61847 -74.02123 360470152003001
    2 40.71348 -73.96551 360470551001009
    3 40.69948 -73.96104 360470537002011
    4 40.70377 -73.93116 360470425003000
    5 40.67859 -73.99049 360470077001000
    6 40.71234 -73.92416 360470449004075

As I understand it, the 15 digit code is several codes put together (the first two being the state, next three the county, and the following six the tract). To get just the census tract code I'd just use the substr function to pull out those six digits.

    > coord$census_tract <- substr(coord$census_code, 6, 1)
    > coord
           lat      long     census_code census_tract
    1 40.61847 -74.02123 360470152003001       015200
    2 40.71348 -73.96551 360470551001009       055100
    3 40.69948 -73.96104 360470537002011       053700
    4 40.70377 -73.93116 360470425003000       042500
    5 40.67859 -73.99049 360470077001000       007700
    6 40.71234 -73.92416 360470449004075       044900

I hope that helps!



来源:https://stackoverflow.com/questions/51499410/retrieve-census-tract-from-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!