R转换zipcode或lat/long到县

scr*_*Owl 23 geocoding r geolocation

我有一个位置列表,其中包含每个位置的城市,州,邮政编码,纬度和经度.

我单独列出了县级经济指标.我玩过zipcode包,ggmap包和其他几个免费地理编码网站,包括美国Gazeteer文件,但似乎无法找到匹配这两个部分的方法.

目前是否有任何包或其他来源这样做?

scr*_*Owl 20

我最终使用了JoshO'Brien上面提到的建议并在此处找到.

我把他的代码,改变statecounty如下所示:

library(sp)
library(maps)
library(maptools)

# The single argument to this function, pointsDF, is a data.frame in which:
#   - column 1 contains the longitude in degrees (negative in the US)
#   - column 2 contains the latitude in degrees

latlong2county <- function(pointsDF) {
    # Prepare SpatialPolygons object with one SpatialPolygon
    # per county
    counties <- map('county', fill=TRUE, col="transparent", plot=FALSE)
    IDs <- sapply(strsplit(counties$names, ":"), function(x) x[1])
    counties_sp <- map2SpatialPolygons(counties, IDs=IDs,
                     proj4string=CRS("+proj=longlat +datum=WGS84"))

    # Convert pointsDF to a SpatialPoints object 
    pointsSP <- SpatialPoints(pointsDF, 
                    proj4string=CRS("+proj=longlat +datum=WGS84"))

    # Use 'over' to get _indices_ of the Polygons object containing each point 
    indices <- over(pointsSP, counties_sp)

    # Return the county names of the Polygons object containing each point
    countyNames <- sapply(counties_sp@polygons, function(x) x@ID)
    countyNames[indices]
}

# Test the function using points in Wisconsin and Oregon.
testPoints <- data.frame(x = c(-90, -120), y = c(44, 44))

latlong2county(testPoints)
[1] "wisconsin,juneau" "oregon,crook" # IT WORKS
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我如何修改这段代码以将lat-long转换为FIPS代码?谢谢. (2认同)

Ric*_*rta 8

匹配Zipcodes到县是困难的.(某些邮政编码跨越一个以上的县,有时甚至超过一个州.例如30165)

我不知道任何特定的R包可以匹配这些.

但是,您可以从密苏里州人口普查数据中心获得一张漂亮的桌子.
您可以使用以下内容进行数据提取:http://bit.ly/S63LNU

示例输出可能如下所示:

    state,zcta5,ZIPName,County,County2
    01,30165,"Rome, GA",Cherokee AL,
    01,31905,"Fort Benning, GA",Russell AL,
    01,35004,"Moody, AL",St. Clair AL,
    01,35005,"Adamsville, AL",Jefferson AL,
    01,35006,"Adger, AL",Jefferson AL,Walker AL
    ...
Run Code Online (Sandbox Code Playgroud)

注意County2.元数据说明可以在这里找到.

    county 
    The county in which the ZCTA is all or mostly contained. Over 90% of ZCTAs fall entirely within a single county.

    county2 
    The "secondary" county for the ZCTA, i.e. the county which has the 2nd largest intersection with it. Over 90% of the time this value will be blank.
Run Code Online (Sandbox Code Playgroud)

另见ANSI县代码 http://www.census.gov/geo/www/ansi/ansi.html


小智 5

我认为"非共识"一揽子计划是有帮助的.

相应的是我用来匹配zipcode与县

### code for get county based on zipcode

library(noncensus)
data(zip_codes)
data(counties)

state_fips  = as.numeric(as.character(counties$state_fips))
county_fips = as.numeric(as.character(counties$county_fips))    
counties$fips = state_fips*1000+county_fips    
zip_codes$fips =  as.numeric(as.character(zip_codes$fips))

# test
temp = subset(zip_codes, zip == "30329")    
subset(counties, fips == temp$fips)
Run Code Online (Sandbox Code Playgroud)

  • 看起来 noncensus 已从 CRAN 1/16/2020 中删除 (3认同)
  • 您的前三行,即 `counties$fips` 的构造,可以在一行中使用 `counties$fips &lt;-interaction(counties$state_fips,counties$county_fips)` 获得。 (2认同)