lundi 1 décembre 2014

How to merge an excel file with a shapefile in r


I am new to mapping in R so my question might look trivial. 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 dont have polygon coordinates for my areas, I decided to use the zip3 shapefile which is available on 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 beilive this is the right approach but it is not working. My problem is that maybe Im reading my shape file to a wrong data frame. I used readShapePoly() to read my shapefile. any hint is appreciated.


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






Aucun commentaire:

Enregistrer un commentaire