如何通过 R 中的州邮政编码信息查找相邻州

How to Find Neighboring States through State Zip code information in R

我想通过 R 中的邮政编码信息找到美国给定特定州的邻近州,谁能帮我解决这个问题?

提前致谢!

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

# 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"