2014-07-11 98 views
0

我想根据它们在R中的距离将单个点捕捉到其他点。详细地说,我有一组由X和Y坐标对定义的点。 另外我有单一的不同点,我想要拍到最近的邻居(欧几里德距离) 。根据坐标获取最近点

# target points 
df <- data.frame(X=c(1,2,2,2,3,4),Y=c(1,2,2,3,3,4)) 

# points that need snapping 
point1 <- data.frame(X=2.1, Y=2.3) 
point2 <- data.frame(X=2.5, Y=2.5) 


plot(df) 
points(point1$X,point1$Y,pch=20,col="red") 
points(point2$X,point2$Y,pch=20,col="blue") 

但是如何进行点的捕捉? 如何捕捉点并将新坐标对分配给单点? R有没有简单的功能?或者是否需要应用 dist()函数来获取距离矩阵并搜索最近距离为 ?也许有更直接的方法。

那应该如何看起来像:

1)捕捉到最近的(欧氏距离)点(澄清溶液为1点)

point1$X_snap <- 2 
point1$Y_snap <- 2 

2)如果两个或多个点相若方式紧密比 捕捉到那个更“东北” a)先捕捉到更多的北(Y方向) b)如果有不止一个,在Y方向上有一个类似的距离捕捉 到那个更多东部

point2$X_snap <- 3 
point2$Y_snap <- 3 

为了图解说明的结果应该如何看起来像

#plot snapped points: 
points(point1$X_snap,point1$Y_snap,pch=8,col="red") 
points(point2$X_snap,point2$Y_snap,pch=8,col="blue") 

回答

0

我会把标准(距离,“southness”,“westness”)的数据帧,然后进行排序沿着这些标准,这个数据帧:

# input data 
df <- data.frame(X=c(1,2,2,2,3,4),Y=c(1,2,2,3,3,4)) 
point1 <- data.frame(X=2.1, Y=2.3) 
point2 <- data.frame(X=2.5, Y=2.5) 
df.res[with(df.res, order(dst, dy, dx)), ] 

# function that sorts all potential snapping points according to distance, "westness", "southness" 
snap.xy <- function(point, other.points) { 
    df.res <- data.frame(X = other.points$X, # to later access the coordinates to snap to 
         Y = other.points$Y, # dto 
         dx <- point$X - other.points$X, # "westness" (the higher, the more "west") 
         dy <- point$Y - other.points$Y, # "southness" 
         dst = sqrt(dx^2 + dy^2))  # distance 
    # print(df.res[with(df.res, order(dst, dy, dx)), ]) # just for checking the results 
    return(df.res[with(df.res, order(dst, dy, dx)), ][1,c("X", "Y")]) # return only the X/Y coordinates 
} 

# examples 
snap.xy(point1, df) # 2/2 
snap.xy(point2, df) # 3/3 
snap.xy(point2, df)$X # 3 
snap.xy(point2, df)$Y # 3 
0

它使用dist可能:

dist(rbind(point1,df)) 
      1   2   3   4   5 
2 1.7029386           
3 0.3162278 1.4142136        
4 0.3162278 1.4142136 0.0000000      
5 0.7071068 2.2360680 1.0000000 1.0000000   
6 1.1401754 2.8284271 1.4142136 1.4142136 1.0000000 
7 2.5495098 4.2426407 2.8284271 2.8284271 2.2360680 
      6 
2   
3   
4   
5   
6   
7 1.4142136 

因此,第一列中具有最小值(距离)的行标识了df中最接近point1的点。在你的例子中,你有一个重复的位置。重复您的每个point_x

2

我发现从BIOBASE(Bioconductor的)使用matchpt()函数 另一种解决方案:

# target points 
df <- data.frame(X=c(1,2,2,2,3,4),Y=c(1,2,2,3,3,4)) 

# points that need snapping 
point1 <- data.frame(X=2.1, Y=2.3) 
point2 <- data.frame(X=2.5, Y=2.5) 

snap <- function(df,point){ 
    require(Biobase) 
    d <- matchpt(as.matrix(df), 
       as.matrix(data.frame(X=point$X+0.0001,Y=point$Y+0.0001))) # to the "northwest" criteria correct 

    min_row <- as.numeric(rownames(d[d$distance==min(d$distance),])) 

    point$X_snap <- unique(df[min_row,"X"]) 
    point$Y_snap <- unique(df[min_row,"Y"]) 

    point 
} 

snap(df,point2) 
0

你可能也想尝试RANN package的快速最近邻搜索:

# your data 
df <- data.frame(X=c(1,2,2,2,3,4),Y=c(1,2,2,3,3,4)) 
pts <- data.frame(X=c(2.1, 2.5), Y=c(2.3, 2.5)) 
library(RANN) 
# for each point in pts, find the nearest neighbor from df 
closest <- RANN::nn2(data = df, query = pts, k = 1) 
# argument k sets the number of nearest neighbours, here 1 (the closest) 
closest 
# $nn.idx 
# [,1] 
# [1,] 3 
# [2,] 5 
# 
# $nn.dists 
# [,1] 
# [1,] 0.3162278 
# [2,] 0.7071068 
# Get coordinates of nearest neighbor 
pts$X_snap <- df[closest$nn.idx, "X"] 
pts$Y_snap <- df[closest$nn.idx, "Y"] 
pts 
#  X Y X_snap Y_snap 
# 1 2.1 2.3  2  2 
# 2 2.5 2.5  3  3