从坐标检索人口普查区 [英] Retrieve Census tract from Coordinates
问题描述
我有一个包含经度和纬度坐标的数据集.我想检索相应的人口普查区.是否有可以允许我执行此操作的数据集或api?
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?
我的数据集如下:
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
推荐答案
tigris
程序包包含一个名为call_geolocator_latlon
的函数,该函数应该可以执行您想要的操作.这是一些使用
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
据我所知,这15位数字是几个数字的总和(前两个是州,接下来的三个是县,后面的六个是区域).要仅获取人口普查区代码,只需使用substr
函数提取这六位数字即可.
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
希望对您有帮助!
这篇关于从坐标检索人口普查区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!