I have two shapefiles. I would like to identify the part of the first shapefile that doesn't overlap with the second shapefile. Then I would like to clip that part of the first shapefile and make it its own shapefile to merge with the second shapefile in R. I have tried
union in the raster package and
gIntersection in the rgeos package but haven't had any luck.
After trying to use the answer below from Simbamangu I think the reason the answer is not working for my shapefiles is that the coordinates for my two shapefiles are formatted differently. The answer works with the example data.
Shapefile 1 output from attributes Shapefile 2 output from attributes
$bbox $bbox min max min max x -67.33333 -66.33739 x 639772.8 723467.9 y 40.58634 41.50000 y 4499609.0 4597064.1
Is there a way to format the coordinates so both shapefiles are formatted the same way and could this be the reason gIntersection is not working.
intersect1<-rgeos::gIntersection(shapefile1, shpaefile2) NULL
I figured out to format the coordinates from the 2 shapefiles into a similar format. The gDifference function doesn't seem to be identifying all of the differences between the two shapefiles. See output below.
Output from getting answer to work. The only part of the Shapefile 1 identified as outside of Shapefile 2 is the little sliver of green, but the 2 sections in white should also be identified.
# Create SpatialPolygons objects polygon1 <- readWKT("POLYGON((-180 -20, -140 55, 10 0, -140 -60, -180 -20))") polygon2 <- readWKT("POLYGON((-180 -20, -140 70, 10 0, -140 -60, -180 -20))")
R version 3.3.1 (2016-06-21) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale:  LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252  LC_MONETARY=English_United States.1252  LC_NUMERIC=C  LC_TIME=English_United States.1252 attached base packages:  grid datasets utils stats graphics grDevices methods  base other attached packages:  taRifx.geo_1.0.7 devtools_1.12.0 rgeos_0.3-19  raster_2.5-8 calibrate_1.7.2 spatialEco_0.1-5  spatstat_1.46-1 rpart_4.1-10 nlme_3.1-128  rgdal_1.1-10 maptools_0.8-39 mapdata_2.2-6  maps_3.1.0 sp_1.2-3 car_2.1-2  xlsx_0.5.7 xlsxjars_0.6.1 rJava_0.9-8  plyr_1.8.4 MASS_7.3-45 RODBC_1.3-13  latticeExtra_0.6-28 RColorBrewer_1.1-2 lattice_0.20-33