dataframe - Give an (x,y) pair, how to choose which (x,y)_i pair is the closest - R -


i have data frame (called coors) contains vector of x coordinates , vector of y coordinates.

i have data frame (called pickedpoint) contains specified (x,y) pairs of interest.

the goal associate each coors point nearest pickedpoint. want use euclidean norm (l-2). if @ possible please tidyverse methods.

       coor = data.frame(row = rep(1:96, each = 72),                          col = rep(1:72, times = 96))         pickedpoint = data.frame(pprow = sample(96,10),                                 ppcol = sample(72,10))  

there thread similar posted in python:

how find closest (x, y) position (x,y) position in list?

i have included benchmark answers far:

microbenchmark(cpak(), latemail(),jul(), times=10l) unit: milliseconds expr       min         lq       mean     median         uq       max neval cpak()  37.83691   38.60585  43.66030   39.86094   44.9592     62.784 10 latemail() 4275.10 4536.783   4674.966   4712.938  4855.860   5045.069 10 jul()   37.38809   39.87625   46.17202   44.90693   53.08938    57.33  10 

i'd suggest using sp package this

library(sp) library(dplyr)  coor$closest <- spdists(as.matrix(coor),as.matrix(pickedpoint)) %>% apply(1,which.min) 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -