# Remove intersect of one polygon from another in R

I am trying to remove the intersect from Poly2 onto Poly1. Below are the coords of the 2 polygons.

> coords1
[,1]     [,2]
[1,] 992.0161 7462.531
[2,] 950.1962 7413.532
[3,] 902.3632 7421.207
[4,] 706.1985 7472.378
[5,] 654.0139 7711.760
[6,] 657.5960 7726.950
[7,] 786.2667 7844.380
[8,] 789.5935 7842.925
[9,] 981.7046 7498.659
[10,] 983.4246 7493.271
[11,] 990.6680 7469.888
[12,] 992.0161 7462.531
> coords2
[,1]     [,2]
[1,] 930.3464 7607.313
[2,] 979.3528 7502.470
[3,] 865.8662 7484.337
[4,] 850.5665 7594.947
[5,] 930.3464 7607.313

Poly1<-Polygon(coords1)
Poly2<-Polygon(coords2)

The 2 polygons looks like this What I would like to get at the end is the dark blue polygon (Poly1) without the light blue (Poly2) area i.e., new eastern coordinates.

-

Try the gDifference function from the rgeos package:

992.0161 7462.531
950.1962 7413.532
902.3632 7421.207
706.1985 7472.378
654.0139 7711.760
657.5960 7726.950
786.2667 7844.380
789.5935 7842.925
981.7046 7498.659
983.4246 7493.271
990.6680 7469.888
992.0161 7462.531
")))

930.3464 7607.313
979.3528 7502.470
865.8662 7484.337
850.5665 7594.947
930.3464 7607.313
")))

library("sp")
poly1 <- Polygon(coords1)
poly2 <- Polygon(coords2)
# create SpatialPolygons objects
p1 <- SpatialPolygons(list(Polygons(list(poly1), "p1")))
p2 <- SpatialPolygons(list(Polygons(list(poly2), "p2")))

library("rgeos")
res <- gDifference(p1, p2)
plot(res, col="blue")

-