0

I have two SpatialPixelDataFrames (a and b) which have few common coordinates/locations. I need to identify these common coordinates and remove them from both the dataframes.

Though I have been able to identify and separate common coordinate points (an2 and bn2), but I feel my code is very inefficient. Is there is better way to do it. I have shared my code here.

Secondly, I am also not sure about how can I remove these locations from the original dataframes "a" and "b".

Thank You

# separating the points with same coordinates 

a<-data.frame(x=c(1,4,6,3,2),y=c(1,3,5,2,9),z=c(5,6,7,8,3))
b<-data.frame(x=c(2,4,2,3,4),y=c(9,3,6,7,4),z=c(3,6,3,4,9))
gridded(a) = ~x+y
gridded(b) = ~x+y

# comparing x coordinates
res <- outer(a@coords[,1], b@coords[,1], `==`)

# an and bn have common x coordinates
index.temp<-which(res,arr.ind = T)
an<-a[unique(index.temp[,1]),]
bn<-b[unique(index.temp[,2]),]

#comparing y coordinates
res <- outer(an@coords[,2], bn@coords[,2], `==`)
index.temp<-which(res,arr.ind = T)

#an2 and bn2 have comon x and y coordinates
an2<-an[unique(index.temp[,1]),]
bn2<-bn[unique(index.temp[,2]),]
Saubhagya
  • 51
  • 1
  • 4
  • I just realized the logic used in my code is not correct as it does not compare (x,y) as ordered pair, rather it first compare x and then y, which can potentially give wrong answer. – Saubhagya Mar 26 '17 at 17:36
  • I realized "merge" function does the above-mentioned job pretty well. – Saubhagya Apr 28 '17 at 14:38

0 Answers0