如何通过R中的状态邮政编码信息查找邻居状态

时间:2016-05-14 11:40:25

标签: r geocoding zipcode

我希望通过R中的邮政编码信息找到美国某个特定州的邻国,有人可以帮助我如何继续吗?

提前感谢!

1 个答案:

答案 0 :(得分:0)

获取邮政编码列表,将其与状态匹配,获取状态形状文件并计算邻居。基本上它可以像这样工作:

# get data
library(spdep)
library(raster)
# https://www.census.gov/geo/reference/codes/cou.html
fips <- read.csv("http://www2.census.gov/geo/docs/reference/codes/files/national_county.txt", header=F, col.names = c("STATE", "STATEFP", "COUNTYFP", "COUNTYNAME", "CLASSFP"))
map <- getData("GADM", country="US", level=2)

# select by code
(county <- as.character(subset(fips, STATE=="NY" & STATEFP==36 & COUNTYFP==61, select=COUNTYNAME)[, 1]))
# [1] New York County

# get neighbour:
nbs <- poly2nb(map) # takes some time to get the neighbour counties
nam <- with(map@data, paste(NAME_2, TYPE_2))
lst <- setNames(lapply(unclass(nbs), function(x) nam[x]), nam)
lst[county]
# $`New York County`
# [1] "Bergen County" "Hudson County" "Bronx County"