The unionSpatialPolygons
function in the maptools
package does this. You supply to it a SpatialPolygons
and a vector which indicates which polygons (zip codes, in your example) belong to which aggregated polygon (sales areas).
EDIT:
The help page on unionSpatialPolygons has a good example of creating a grouping/indexing vector. It using counties in North Carolina, but the ideas are the same. Pulling from that example:
nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=NAD27"))
Loads an example SpatialPolygonsDataFrame
. Extract from this whatever information you need that is one-per-polygon. The example gets single coordinates associated with the polygon (centroid?)
lps <- coordinates(nc1)
You can see these together:
plot(nc1)
points(lps)
You could do something with nc1@data
instead. Either have 100 entries, one for each polygon.
> dim(nc1@data)
[1] 100 14
> dim(lps)
[1] 100 2
> length(nc1@polygons)
[1] 100
Then use whatever transformation/mapping to derive a length 100 vector that indicates the groups.
The gpclibPermitStatus
error probably means you don't have the rgeos
package installed and/or have not agreed to the more restrictive license of gpclib.