Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map zip codes to their respective city and state in R?

Tags:

r

zipcode

I have a data frame of zip codes that I'm looking to map to a city & state for each specific zip code. Currently, I have played around with the zipcode package a bit but I'm not sure that can solve this specific issue.

Here's sample data of what I have now:

str(all_key$zip)
chr [1:406] "43031" "24517" "43224" "43832" "53022" "60185" "84104" "43081" 
"85226" "85193" "54656" "43215" "94533" "95826" "64804" "49548" "54467" 

The expected output would be adding a city & state column to each row of the data frame referring to the individual zips:

 head(all_key)
     zip city  state
1   43031 city1 state1
2   24517 city2 state2
3   43224 city3 state3
4   43832 city4 state4
5   53022 city5 state5
6   60185 city6 state6

Thanks in advance for your help.

like image 354
Franchise Avatar asked Jun 13 '18 20:06

Franchise


People also ask

How do I use zipcode in R?

If you are not familiar with R, the first line is loading the 'zipcode' package into the current R session. The second line is using 'data' function to extract the 'zipcode' data from the package as a data frame called 'zipcode'. The last line is calling the data frame to return the data.

Can you map ZIP codes on Google Maps?

Google Maps has added a feature where it will highlight in a pink color the borders of a city, postal code or other borders based on your search. To see it yourself, go to Google Maps and search for a city name or even a zip code. You will see a pinkish highlight around the border.

How do I pull a zip code from a map?

Click on the map to start drawing an area. Once you have defined the area in which you wish to search, click the [Search For Zip Codes] button. After a small delay, the results will appear inside the area. If you hover over or tap a marker, you will see its ZIP code.

What is zip map?

ZIP code mapping is a method of analyzing data in relation to ZIP codes. It is usually used in comparison analysis and territory definition. As the most granular boundary dataset, it allows a more exact analysis of business data.


1 Answers

Answer updated

The zipcode package seems to have disappeared, so this answer has been updated to show how to add lat-lon from an external file. New answer at bottom.


You can get the data from the zipcode package and just do a merge to look things up.

zip = c("43031", "24517", "43224", "43832", "53022", 
 "60185", "84104", "43081", "85226", "85193", "54656", 
 "43215", "94533", "95826", "64804", "49548", "54467")
ZC = data.frame(zip)

library(zipcode)
data(zipcode)
merge(ZC, zipcode)
     zip           city state latitude  longitude
1  24517      Altavista    VA 37.12754  -79.27409
2  43031      Johnstown    OH 40.15198  -82.66944
3  43081    Westerville    OH 40.10951  -82.91606
4  43215       Columbus    OH 39.96513  -83.00431
5  43224       Columbus    OH 40.03991  -82.96772
6  43832  Newcomerstown    OH 40.27738  -81.59662
7  49548   Grand Rapids    MI 42.86823  -85.66391
8  53022     Germantown    WI 43.21916  -88.12043
9  54467         Plover    WI 44.45228  -89.54399
10 54656         Sparta    WI 43.96977  -90.80796
11 60185   West Chicago    IL 41.89198  -88.20502
12 64804         Joplin    MO 37.04716  -94.51124
13 84104 Salt Lake City    UT 40.75063 -111.94077
14 85193    Casa Grande    AZ 32.86000 -111.83000
15 85226       Chandler    AZ 33.31221 -111.93177
16 94533      Fairfield    CA 38.26958 -122.03701
17 95826     Sacramento    CA 38.55010 -121.37492

If you need to keep the rows in the same order, you can just set the rownames on the zipcode data and use that to select the desired rows and columns.

rownames(zipcode) = zipcode$zip
zipcode[zip, 1:3]
        zip           city state
43031 43031      Johnstown    OH
24517 24517      Altavista    VA
43224 43224       Columbus    OH
43832 43832  Newcomerstown    OH
53022 53022     Germantown    WI
60185 60185   West Chicago    IL
84104 84104 Salt Lake City    UT
43081 43081    Westerville    OH
85226 85226       Chandler    AZ
85193 85193    Casa Grande    AZ
54656 54656         Sparta    WI
43215 43215       Columbus    OH
94533 94533      Fairfield    CA
95826 95826     Sacramento    CA
64804 64804         Joplin    MO
49548 49548   Grand Rapids    MI
54467 54467         Plover    WI


Updated Answer

Since the zipcode package has disappeared, this shows how to add lat-lon information from a downloaded data set. The file that I am using exists today but the method should work for other files. See the GIS StackExchange for some leads on where to download data.

## Original Data to match
zip = c("43031", "24517", "43224", "43832", "53022", 
 "60185", "84104", "43081", "85226", "85193", "54656", 
 "43215", "94533", "95826", "64804", "49548", "54467")
ZC = data.frame(zip)

## Download source file, unzip and extract into table
ZipCodeSourceFile = "http://download.geonames.org/export/zip/US.zip"
temp <- tempfile()
download.file(ZipCodeSourceFile , temp)
ZipCodes <- read.table(unz(temp, "US.txt"), sep="\t")
unlink(temp)
names(ZipCodes) = c("CountryCode", "zip", "PlaceName", 
"AdminName1", "AdminCode1", "AdminName2", "AdminCode2", 
"AdminName3", "AdminCode3", "latitude", "longitude", "accuracy")

## merge extra info onto original data
fZC_Info = merge(ZC, ZipCodes[,c(2:6,10:11)])
head(ZC_Info)
    zip     PlaceName AdminName1 AdminCode1 AdminName2 latitude longitude
1 24517     Altavista   Virginia         VA   Campbell  37.1222  -79.2911
2 43031     Johnstown       Ohio         OH    Licking  40.1445  -82.6973
3 43081   Westerville       Ohio         OH   Franklin  40.1146  -82.9105
4 43215      Columbus       Ohio         OH   Franklin  39.9671  -83.0044
5 43224      Columbus       Ohio         OH   Franklin  40.0425  -82.9689
6 43832 Newcomerstown       Ohio         OH Tuscarawas  40.2739  -81.5940
like image 50
G5W Avatar answered Oct 24 '22 21:10

G5W