I am new to mapping in R.

I want to have a density map of US for number of phone calls for different areas. Each area includes some zip codes. I only have number of calls for each zip code and I want to show it on a map of US by using zip codes. As I don't have polygon coordinates for my areas, I decided to use the zip3 shapefile which is available on the web and merge my data with the existing shapefile.

To be more clear, my area is formed by zips 006, 010 and 011. I defined an ID for each area and if different zips are related to the same area, they will have identical ids. Hence, my Excel file includes area name, zip3 and ids related to each zip3. I used zip3 from my Excel file as a joining variable to the shapefile (shapefile has a zip3 column) and allocated different ids to zip3s which are in the shapefile.

At this time, I wanted to merge polygons of zip3 s which forms an area to build a map of my areas from the shapefile of zip3s. I believe this is the right approach but it is not working. My problem is that maybe I'm reading my shapefile to a wrong data frame. I used readShapePoly() to read my shapefile.

My code is as follows:

data <- read.csv(“zipmatch.csv”,colClasses=c(“character”,”character”,”numeric”),header=TRUE)
data2 <- readShapePoly(“zip3.shp”)
data3 <- merge(data2,data,by="ZIP3")
final <- unionSpatialPolygons(data3,"id")

It gives me the following error at this time:

Error in unionSpatialPolygons(data3, "id") : input lengths differ

closed as off-topic by PolyGeo Aug 8 '17 at 21:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Is this something you're going to do more than once, ie. you want to automate it with R, or will you be doing it only once, and hence you could use QGIS to put this data together before using it in your analysis? – DPSSpatial Dec 1 '14 at 20:40
  • I think it will be done once. Once I have a map of my desired areas polygons, I will do different analysis with that. I will look at QGIS to see how it works. Thanks – Fairy Dec 1 '14 at 20:43
  • 1
    Provide the code you are using and more detail about the files. This is pretty straightforward using tools in sp. Also, generally it's better to use rgdal::readOGR rather than maptools::readShapePoly since you'll also get projection metadata automatically (if it's there). – mdsumner Dec 1 '14 at 20:46
  • @Fairy hopefully someone can help you do this with R, but this is an easy thing to do with a desktop GIS... – DPSSpatial Dec 1 '14 at 21:24
  • 1
    is nrow(data) == nrow(data1) == nrow(data3)? There is a chance that you are loosing rows in the merge step if the ZIP3 lists are not the same. Then you could try merge(data2,data,by="ZIP3", all=TRUE) – yellowcap Dec 2 '14 at 8:28

Browse other questions tagged or ask your own question.