29

各場所の都市、州、郵便番号、緯度、経度を含む場所のリストがあります。

郡レベルの経済指標のリストを別に持っています。zipcodeパッケージ、パッケージ、および US Gazeteer ファイルを含む他のいくつかの無料のジオコーディング Web サイトで遊んでみましたがggmap、2 つの部分を一致させる方法が見つからないようです。

現在、これを行うパッケージまたは他のソースはありますか?

4

4 に答える 4

22

JoshO'Brien上記の提案を使用して終了し、hereを見つけました。

私は彼のコードを取り、ここに示すように変更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
于 2012-11-20T13:36:45.470 に答える
9

郵便番号を郡に一致させることは困難です。(特定の郵便番号は、複数の郡、場合によっては複数の州にまたがっています。たとえば、30165)。

これらに一致する特定のRパッケージを認識していません。

ただし、Missouri Census Data Center から素敵なテーブルを入手できます。
データ抽出には以下を使用できます: 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
    ...

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.

ANSI郡コードも参照 http://www.census.gov/geo/www/ansi/ansi.html

于 2012-11-09T22:19:01.303 に答える
7

パッケージ「noncensus」が役立つと思います。

対応するのは、郵便番号と郡を一致させるために使用するものです

### 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)
于 2014-12-01T21:37:02.700 に答える
3

簡単なオプションは、オプションまたはを使用してgeocode()関数 inを使用することです。ggmapoutput="more"output="all

これは、住所や緯度/経度などの柔軟な入力を受け取ることができ、住所、都市、郡、州、国、郵便番号などをリストとして返します。

require("ggmap")
address <- geocode("Yankee Stadium", output="more")

str(address)
$ lon                        : num -73.9
$ lat                        : num 40.8
$ type                       : Factor w/ 1 level "stadium": 1
$ loctype                    : Factor w/ 1 level "approximate": 1
$ address                    : Factor w/ 1 level "yankee stadium, 1 east 161st street, bronx, ny 10451, usa": 1
$ north                      : num 40.8
$ south                      : num 40.8
$ east                       : num -73.9
$ west                       : num -73.9
$ postal_code                : chr "10451"
$ country                    : chr "united states"
$ administrative_area_level_2: chr "bronx"
$ administrative_area_level_1: chr "ny"
$ locality                   : chr "new york"
$ street                     : chr "east 161st street"
$ streetNo                   : num 1
$ point_of_interest          : chr "yankee stadium"
$ query                      : chr "Yankee Stadium"

別の解決策は、国勢調査シェープファイルとover()、質問と同じコマンドを使用することです。maptools ベース マップの使用中に問題が発生しました。WGS84 データムを使用しているため、北米では、海岸から数マイル以内にあるポイントが正しくマッピングされず、データ セットの約 5% が一致しませんでした。

spパッケージと Census TIGERLine 形状ファイルを使用して、これを試してください

counties <- readShapeSpatial("maps/tl_2013_us_county.shp", proj4string=CRS("+proj=longlat +datum=NAD83"))

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

countynames <- over(pointsSP, counties)
countynames <- countynames$NAMELSAD
于 2013-11-21T20:56:44.803 に答える