My goal is to modify an existing shapefile by merging certain polygons.

After importing the shapefile and using the UnionSpatialPolygons command, I get the polygon outline that I want.

However, this is now a SpatialPolygons object and not a SpatialPolygonsDataFrame, so I'm unable to export it to a shapefile using writeOGR.

How can I get around this problem?

  • 3
    If the answer below was useful, you should select it as the correct one by clicking on the tick mark to the left of the answer text. Jun 5, 2013 at 17:37

2 Answers 2


As the name says, a SpatialPolygonsDataFrame is basically just a SpatialPolygons object with data attached (the attribute table). The data must have at least as many rows as there are features

ob <- SpatialPolygons(..)# Your SpatialPolygons Object
spp <-     SpatialPolygonsDataFrame(ob,data=as.data.frame("yourData"),proj4string=CRS("+proj=    aea > +ellps=GRS80 +datum=WGS84"))
writeOGR(spp,"shapes","testShape",driver="ESRI Shapefile",)


If you want to convert your SpatialPolygonsDataFrame back to a SpatialPolygons object you just need to address the object structure within R

ob <- SpatialPolygons(spp@polygons,proj4string=spp@proj4string)
  • For sure, @Curlew is right. Make sure to specify IDs argument correctly when executing UnionSpatialPolygons. Otherwise you might get trouble specifying data argument when converting back to SpatialPolygonsDataFrame.
    – fdetsch
    May 23, 2013 at 13:18
  • 1
    Sure, quite easy. I edited the original answer for that
    – Curlew
    Mar 12, 2014 at 23:07
  • 1
    I only recently started working with shapefiles and still trying to familiarise myself with this. What exact is should be substituted for youData in data=as.data.frame("yourData")? After dissolving inner polygons using unionSpatialPolygons(...), I would like to write result as new shapefile Nov 8, 2014 at 2:54
  • You need to have a data.frame with the same number of rows as you have features in your SpatialPolygons object. Better ask a new question regarding you union issue..
    – Curlew
    Nov 8, 2014 at 11:56
  • 1
    @Curlew Thank you, just posted new questions here gis.stackexchange.com/q/121405/40108 Nov 8, 2014 at 14:05


1: the outcome of UnionSpatialPolygons is a spatial polygon

2: converting the result back into a spatial polygon data frame is a real pain

-a. you need a very exact data frame to attach to a spatial polygon

-b. data you used for UnionSpatialPolygons has more rows than the output and is not formatted in the way that is needed.

My (ugly) solution:

### Coerce into spatial polygon data frame with id and row name of spatial polygon

# Make a data frame that meets the requirements above:

df<- data.frame(id = getSpPPolygonsIDSlots(your.spatialpolygon))
row.names(df) <- getSpPPolygonsIDSlots(your.spatialpolygon)

# Make spatial polygon data frame
spdf <- SpatialPolygonsDataFrame(your.spatialpolygon, data =df)

# Then don't forget to make sure the projection is correct
# XXXX is your SRID

proj4string(spdf) <- CRS("+init=epsg:XXXX");
spdf <- spTransform(spdf , CRS("+init=epsg:XXXX"));
  • Error in if (length(Sr@polygons) != nrow(data)) stop(paste("Object length mismatch:\n ", : argument is of length zero In addition: Warning messages: 1: use *apply and slot directly
    – Mox
    Mar 16, 2018 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.