I am having trouble trying to merge two shapefiles in R. One of them has 5 polygons and the other one has 1, so I would like to obtain a final shapefile with 6 polygons.

I try to replicate the merge function from ArcGIS, unsuccessfully though. I have tried with several functions (gUnion, unión from raster package, merge), but all of them create more columns instead of adding a new row.

Could anybody help me to solve this issue?

I have tried with the methods proposed in Merging multiple SpatialPolygonDataFrames into 1 SPDF in R? but they do not work in my case.

  • 2
    You need to provide information, beyond "it does not work". What have you tried, where is your code, what error are your receiving? Have you tried rbind, raster::union or raster::intersect? If your data have non-matching columns then it would be expected that they are included in the output. What does the resulting polygon topology look like and what are the new dimensions of your data. If the polygons intersect the new polygons should be created otherwise only the new feature will be added. I am assuming that the data are in the same projection space. – Jeffrey Evans Dec 5 '17 at 17:27
  • Show us your code that reads in the shapes. Show us summary(them) so we can see what you've got (or share your shapefiles). Show us what you get when you try those methods. Show us code. Code is reproducible. – Spacedman Dec 5 '17 at 17:44

You can use raster::bind. That function combines the geometries and the attributes, even if the variable names do not match.

Example data:

p <- shapefile(system.file("external/lux.shp", package="raster"))
p1 <- p[p$NAME_2=='Mersch', ]
p2 <- p[p$NAME_2=='Diekirch', ]
p2$NAME_1 <- NULL

Use of bind:

x <- bind(p1, p2)

You can use union, but that is intended for combining geometries of overlapping polygons. The resulting attributes are different. I seems that bind is what you want.


You might consider using the union function from the raster package. The following example demonstrates how you can read in two polygon shapefiles and union them together. In this example, the first shapefile contains one polygon and the second contains three polygons.

##load libraries

Next read in your shapefiles using readOGR from rgdal

#read in first polygon shapefile
subs1 <- readOGR('sub1.shp')

#read in second polygon shapefile
subs2 <- readOGR('sub2.shp')

Run a summary to check that they have loaded correctly:

Object of class SpatialPolygonsDataFrame
       min      max
x 563566.8 564092.6
y 613293.4 613595.3
Is projected: TRUE 
proj4string :
[+proj=tmerc +lat_0=53.5 +lon_0=-8 +k=0.99982 +x_0=600000 +y_0=750000
+ellps=GRS80 +units=m +no_defs]
Data attributes:
 Min.   :2008   CHF:1      NS:1       Min.   :2003   12344A3:1  
 1st Qu.:2008                         1st Qu.:2003              
 Median :2008                         Median :2003              
 Mean   :2008                         Mean   :2003              
 3rd Qu.:2008                         3rd Qu.:2003              
 Max.   :2008                         Max.   :2003              

Object of class SpatialPolygonsDataFrame
      min      max
x 563599.3 564704.3
y 612977.6 613588.5
Is projected: TRUE 
proj4string :
[+proj=tmerc +lat_0=53.5 +lon_0=-8 +k=0.99982 +x_0=600000 +y_0=750000
+ellps=GRS80 +units=m +no_defs]
Data attributes:
Min.   :2010    CHF:3      SP:3       Min.   :2002   12345A1:1  
1st Qu.:2010                          1st Qu.:2004   12345A2:1  
Median :2012                          Median :2005   12345B1:1  
Mean   :2011                          Mean   :2005              
3rd Qu.:2013                          3rd Qu.:2007              
Max.   :2014                          Max.   :2009        

Generate a simple plot to show the spatial location of the features:

##plot the inputs:
plot(subs2, col='blue')
plot(subs1, add=TRUE, col='red')

enter image description here

Union the features:

##union the  SpatialPolygonsDataFrame
subs_union <- union(subs1, subs2)

Check the output:

plot(subs_union, col='brown')

enter image description here

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