R 基于坐标获取最近点

R 基于坐标获取最近点,r,coordinates,distance,euclidean-distance,R,Coordinates,Distance,Euclidean Distance,我想根据单个点在R中的距离将它们捕捉到其他点。 具体来说,我有一组由X和Y坐标对定义的点。 此外,我有一个不同的点,我想捕捉到 最近邻(欧几里德距离) 我会将标准(距离、“南”、“西”)放在一个数据框中,然后根据这些标准对该数据框进行排序: # 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

我想根据单个点在R中的距离将它们捕捉到其他点。 具体来说,我有一组由X和Y坐标对定义的点。 此外,我有一个不同的点,我想捕捉到 最近邻(欧几里德距离)

我会将标准(距离、“南”、“西”)放在一个数据框中,然后根据这些标准对该数据框进行排序:

# 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
#输入数据
df我将把标准(距离、“南”、“西”)放在一个数据框中,然后按照这些标准对该数据框进行排序:

# 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
#输入数据

df可以使用
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
的点。在您的示例中,您有一个重复的位置。对每个
点重复上述操作,可以使用
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
的点。在您的示例中,您有一个重复的位置。对每个
点重复上述步骤,我使用matchpt()函数找到了另一个解决方案
来自Biobase(Bioconductor):

#目标点

df我使用matchpt()函数找到了另一个解决方案 来自Biobase(Bioconductor):

#目标点

df您可能还想尝试快速近邻搜索:

# 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
#您的数据

df您可能还想尝试快速近邻搜索:

# 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
#您的数据
df
# 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)
# 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