# matching locations on distance

how i can match all lat and long of one locaton to other depending on distance of 10 kms (one location of one data frame can match all location of other data frame if it lies within 10kms) here is reprex but it can find the nearest only on euclidian distance ,how i can do it please help

``````library(data.table)

df<-structure(list(ID=c(1:5), lat = c(runif(5,33.45,33.75))
, lon = c(runif(5,72.83,73.17)))
, col.Names = c("ID","lat", "lon"), row.names = c(NA, -10L), class = c("data.table","data.frame"))
df
#>    ID      lat      lon
#> 1:  1 33.60637 72.86687
#> 2:  2 33.47437 72.85774
#> 3:  3 33.48841 72.86434
#> 4:  4 33.64597 72.93610
#> 5:  5 33.69246 72.83683
ref<-structure(list(ID=letters[1:5], lat = c(runif(5,33.45,33.75)), lon = c(runif(5,72.83,73.17)))
, col.Names = c("ID","lat", "lon"),row.names = c(NA, -10L), class = c("data.table","data.frame"))
ref
#>    ID      lat      lon
#> 1:  a 33.46918 73.14962
#> 2:  b 33.52341 73.09213
#> 3:  c 33.51321 73.14198
#> 4:  d 33.63886 72.93963
#> 5:  e 33.53759 73.10403
#Setting to data.table format
setDT(df)
setDT(ref)
#creating a table with cartesian join
df1<-setkey(df[,c(k=1,.SD)],k)[ref[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]
df1
#calculating the Euclidean distance and giving a rank in ascending order of distance
df1[,EuDist:=sqrt((lat-i.lat)^2+(lon-i.lon)^2)][,distRank:=rank(EuDist,ties="random"),by=.(ID)]
df1
#>     ID      lat      lon i.ID    i.lat    i.lon     EuDist distRank
#>  1:  1 33.60637 72.86687    a 33.46918 73.14962 0.31427222        5
#>  2:  2 33.47437 72.85774    a 33.46918 73.14962 0.29192180        5
#>  3:  3 33.48841 72.86434    a 33.46918 73.14962 0.28593113        5
#>  4:  4 33.64597 72.93610    a 33.46918 73.14962 0.27720990        5
#>  5:  5 33.69246 72.83683    a 33.46918 73.14962 0.38430701        5
#>  6:  1 33.60637 72.86687    b 33.52341 73.09213 0.24004455        2
#>  7:  2 33.47437 72.85774    b 33.52341 73.09213 0.23945730        2
#>  8:  3 33.48841 72.86434    b 33.52341 73.09213 0.23046342        2
#>  9:  4 33.64597 72.93610    b 33.52341 73.09213 0.19840640        2
#> 10:  5 33.69246 72.83683    b 33.52341 73.09213 0.30619347        2
#> 11:  1 33.60637 72.86687    c 33.51321 73.14198 0.29044877        4
#> 12:  2 33.47437 72.85774    c 33.51321 73.14198 0.28687538        4
#> 13:  3 33.48841 72.86434    c 33.51321 73.14198 0.27874740        4
#> 14:  4 33.64597 72.93610    c 33.51321 73.14198 0.24496862        4
#> 15:  5 33.69246 72.83683    c 33.51321 73.14198 0.35389895        4
#> 16:  1 33.60637 72.86687    d 33.63886 72.93963 0.07968602        1
#> 17:  2 33.47437 72.85774    d 33.63886 72.93963 0.18374985        1
#> 18:  3 33.48841 72.86434    d 33.63886 72.93963 0.16824475        1
#> 19:  4 33.64597 72.93610    d 33.63886 72.93963 0.00793718        1
#> 20:  5 33.69246 72.83683    d 33.63886 72.93963 0.11593820        1
#> 21:  1 33.60637 72.86687    e 33.53759 73.10403 0.24692884        3
#> 22:  2 33.47437 72.85774    e 33.53759 73.10403 0.25426970        3
#> 23:  3 33.48841 72.86434    e 33.53759 73.10403 0.24468675        3
#> 24:  4 33.64597 72.93610    e 33.53759 73.10403 0.19986647        3
#> 25:  5 33.69246 72.83683    e 33.53759 73.10403 0.30883775        3
#>     ID      lat      lon i.ID    i.lat    i.lon     EuDist distRank
#selecting the shortest distance
df1<-df1[distRank==1]
df1
#>    ID      lat      lon i.ID    i.lat    i.lon     EuDist distRank
#> 1:  1 33.60637 72.86687    d 33.63886 72.93963 0.07968602        1
#> 2:  2 33.47437 72.85774    d 33.63886 72.93963 0.18374985        1
#> 3:  3 33.48841 72.86434    d 33.63886 72.93963 0.16824475        1
#> 4:  4 33.64597 72.93610    d 33.63886 72.93963 0.00793718        1
#> 5:  5 33.69246 72.83683    d 33.63886 72.93963 0.11593820        1
``````

Created on 2020-11-12 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.