【问题标题】:How to Find Neighboring States through State Zip code information in R [closed]如何通过 R 中的州邮政编码信息查找邻国 [关闭]
【发布时间】:2016-05-14 11:40:24
【问题描述】:

我想通过 R 中的邮政编码信息来查找美国某个特定州的邻近州,有人可以帮我看看如何继续吗?

提前致谢!

【问题讨论】:

  • 我创建了一个 Excel 电子表格,其中列出了每个州的所有连续州,我可以将其发送给您,但我不知道如何在 R 中使用邮政编码来完成。
  • 欢迎来到 StackOverflow。请阅读 (1) how do I ask a good question, (2) How to create a MCVE 以及 (3) how to provide a minimal reproducible example in R。然后相应地编辑和改进您的问题。即,提供输入数据、预期输出、您尝试了哪些代码行以及它们以何种方式失败。

标签: r geocoding zipcode


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多