0

我有两个 SpatialPixelDataFrames(a 和 b),它们几乎没有共同的坐标/位置。我需要识别这些公共坐标并将它们从两个数据框中删除。

虽然我已经能够识别和分离公共坐标点(an2 和 bn2),但我觉得我的代码效率很低。有没有更好的方法来做到这一点。我在这里分享了我的代码。

其次,我也不确定如何从原始数据框“a”和“b”中删除这些位置。

谢谢你

# 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]),]
4

0 回答 0