【问题标题】:How do I find other zipcodes that touch a particular zipcode?如何找到涉及特定邮政编码的其他邮政编码?
【发布时间】:2014-03-26 05:16:11
【问题描述】:

我想为大约 200 个邮政编码和接触这些邮政编码的相邻邮政编码创建一个矩阵。该矩阵将为 200*200,其中两个邮政编码接触的单元格为 1,如果它们不是相邻的邮政编码,则为 0。

如何创建或获取这样的矩阵?非常感谢。

最好的,

【问题讨论】:

  • 有一个邮政编码 shapefile here,如果有帮助的话。
  • 您想根据什么信息来构建这个?你有形状文件吗?也许是坐标?
  • 我可以访问 jbaums 使用的一些 shapefile。但不是我想要的所有州。我想我可以手动输入邮政编码的坐标,如果它归结为它。你觉得我应该怎么做?我想要 9 个州的信息,我能够获得 4 个州的 shapefile,但不能获得下面提到的其余 5 个州。请让我知道如何使用经纬度信息来创建此矩阵。非常感谢。
  • @danny117 - 您在赏金描述中提出了不同的问题,但我认为对我帖子的修改可能会有所帮助。

标签: r zipcode


【解决方案1】:

如果您可以访问 shapefile,在 spdep 包的帮助下,这相对简单。

这是一个使用加利福尼亚邮政编码数据的独立示例(约 3.5MB 下载):

# load libraries
library(rgdal)
library(spdep)

# download, unzip and import shapefile
download.file('http://geocommons.com/overlays/305142.zip', {f<-tempfile()})
unzip(f, exdir=tempdir())
shp <- readOGR(tempdir(), 'tigerline_shapefile_2010_2010_state_california_2010_census_5-digit_zip_code_tabulation_area_zcta5_state-based')

# identify neighbours for each poly
nbs <- setNames(poly2nb(shp), shp$ZCTA5CE10)

# convert to a binary neighbour matrix
nbs.mat <- nb2mat(nbs, zero.policy=TRUE, style='B')

# see?rgeos::gTouches for an alternative to the above steps

# assign zip codes as dimension names
dimnames(nbs.mat) <- list(shp$ZCTA5CE10, shp$ZCTA5CE10)

对于我们的数据集,这将返回一个 1769 x 1769 矩阵,指示哪些邮政编码是邻居。前 10 行 10 列如下所示:

nbs.mat[1:10, 1:10]

##       94601 94501 94560 94587 94580 94514 94703 95601 95669 95901
## 94601     0     1     0     0     0     0     0     0     0     0
## 94501     1     0     0     0     0     0     0     0     0     0
## 94560     0     0     0     0     0     0     0     0     0     0
## 94587     0     0     0     0     0     0     0     0     0     0
## 94580     0     0     0     0     0     0     0     0     0     0
## 94514     0     0     0     0     0     0     0     0     0     0
## 94703     0     0     0     0     0     0     0     0     0     0
## 95601     0     0     0     0     0     0     0     0     0     0
## 95669     0     0     0     0     0     0     0     0     0     0
## 95901     0     0     0     0     0     0     0     0     0     0

如果您想要一个两列矩阵给出相邻的邮政编码对(即第 1 列中的邮政编码和第 2 列中的相邻邮政编码),您可以使用以下内容。

nbs.list <- sapply(row.names(nbs.mat), function(x) names(which(nbs.mat[x, ] == 1)))

nbs.pairs <- data.frame(zipcode=rep(names(nbs.list), sapply(nbs.list, length)), 
                        neighbour=unlist(nbs.list))

head(nbs.pairs)

##        zipcode neighbour
## 946011   94601     94501
## 946012   94601     94602
## 946013   94601     94605
## 946014   94601     94606
## 946015   94601     94621
## 946016   94601     94619    

【讨论】:

  • 非常感谢 jbaums。这就是我一直在寻找的。 :-) 但是我找不到 KY、MS、NC、SC、TN 和 VA 的形状文件。我应该在哪里可以找到它们。非常感谢。
  • 我不确定它们是否适用于 geocommons 的所有州,但您可以尝试 this 文件,我想该文件包含 所有 州的邮政编码。 (我没有检查,因为它是 500MB 的下载量,我并不特别需要。)
  • 还要注意邮政编码和ZIP code tabulation areas之间的区别。后者在上述 shapefile 中提供。
  • 是的,我确实想要这对。这是我可能会花一些时间的事情。
  • @user3435644 另外:在前者(邮政编码)中,有邮政编码区(~33,000)和邮局/大客户(~10,000)。前者是形状,后者是点。了解与此类分析的区别很重要。
最近更新 更多