duplication of latitude and longitude while searching nearest neighborhood

hey to all m new here, please guide there is duplication of latitude and longitude while searching nearest neighborhood in df1$ i.lat & df1$i.long(5,6,7,8,9) at last two roes highlighted as under
#> [1] 33.67 33.61 33.58 33.61 33.59 33.59 33.57 33.57 33.57 33.64
df1$i.lon
#> [1] 73.14 73.10 73.07 73.07 72.90 72.90 73.14 73.14 73.14 72.95

 library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    set.seed(19)
    store=data.frame(lat=runif(1,33.45,33.75),long=runif(1,72.83,73.17))
    onlineloc=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    
    p=runif(1, 0, 1)
    if(p>0.5){instore=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    Sys.sleep(10)
    print(instore)
    }else{Sys.sleep(10)
      print("NO INSTORE CUSTOMER APPEARED")
    }
#>         lat     long
#> 1  33.70783 73.10150
#> 2  33.66670 73.14288
#> 3  33.60976 73.09793
#> 4  33.57661 73.06561
#> 5  33.60510 73.06707
#> 6  33.67357 72.91224
#> 7  33.59360 72.90426
#> 8  33.56916 73.14372
#> 9  33.63713 72.94552
#> 10 33.53505 72.88051
    #instore=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    instore
#>         lat     long
#> 1  33.70783 73.10150
#> 2  33.66670 73.14288
#> 3  33.60976 73.09793
#> 4  33.57661 73.06561
#> 5  33.60510 73.06707
#> 6  33.67357 72.91224
#> 7  33.59360 72.90426
#> 8  33.56916 73.14372
#> 9  33.63713 72.94552
#> 10 33.53505 72.88051
    instorecustomer=round(instore,digits = 2)
    onlinecustomer=round(onlineloc,digits = 2)
    plot(onlineloc,main="Online and instore customer loc",col=" red")
    points(instore,pch=2)
    points(store,pch=15)
    legend("bottomright",legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))

    #plot(onlineloc,main="Online customer loc",col=" red")
    #results=kmeans(onlineloc,5)
    #results
    #onlinecluster=results$centers[,]
    #onlinecluster=as.data.frame(onlinecluster)
    #onlinecluster=round(onlinecluster,digits = 2)
    
    #distance2=dist(onlinecluster)
    #round(distance2,digits = 2)
    #plot(hclust(distance2))
    onlinecustomer
#>      lat  long
#> 1  33.65 73.10
#> 2  33.47 73.14
#> 3  33.56 72.91
#> 4  33.52 73.14
#> 5  33.54 72.97
#> 6  33.62 73.02
#> 7  33.70 72.98
#> 8  33.67 73.12
#> 9  33.57 73.07
#> 10 33.58 73.16
    instorecustomer
#>      lat  long
#> 1  33.71 73.10
#> 2  33.67 73.14
#> 3  33.61 73.10
#> 4  33.58 73.07
#> 5  33.61 73.07
#> 6  33.67 72.91
#> 7  33.59 72.90
#> 8  33.57 73.14
#> 9  33.64 72.95
#> 10 33.54 72.88
    
    library(sf)
    p1=onlinecustomer%>%st_as_sf(coords = c("lat","long"), crs = 4326)
    p2=instorecustomer%>%st_as_sf(coords = c("lat","long"), crs = 4326)
    #nrow(st_join(onlinecluster, instorecustomer, join = st_equals, left = F)) == nrow(p1)
    i=nrow(st_join(p1,p2, join = st_equals, left = F)) 
    orders=nrow(onlinecustomer) 
    orders=orders-i
    print(paste("No of task to be delivered are",orders))
#> [1] "No of task to be delivered are 10"
    
    library(data.table)
    df<-structure(list(ID=c(1:10), lat = c(onlinecustomer$lat)
                       , lon = c(onlinecustomer$long))
                  , col.Names = c("ID","lat", "lon"), row.names = c(NA, -10L), class = c("data.table","data.frame"))
    df
#>     ID   lat   lon
#>  1:  1 33.65 73.10
#>  2:  2 33.47 73.14
#>  3:  3 33.56 72.91
#>  4:  4 33.52 73.14
#>  5:  5 33.54 72.97
#>  6:  6 33.62 73.02
#>  7:  7 33.70 72.98
#>  8:  8 33.67 73.12
#>  9:  9 33.57 73.07
#> 10: 10 33.58 73.16
    ref<-structure(list(ID=letters[1:10], lat = c(instorecustomer$lat), lon = c(instorecustomer$long))
                   , col.Names = c("ID","lat", "lon"),row.names = c(NA, -10L), class = c("data.table","data.frame"))
    ref
#>     ID   lat   lon
#>  1:  a 33.71 73.10
#>  2:  b 33.67 73.14
#>  3:  c 33.61 73.10
#>  4:  d 33.58 73.07
#>  5:  e 33.61 73.07
#>  6:  f 33.67 72.91
#>  7:  g 33.59 72.90
#>  8:  h 33.57 73.14
#>  9:  i 33.64 72.95
#> 10:  j 33.54 72.88
    #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.65 73.10    a 33.71 73.10 0.06000000        4
#>   2:  2 33.47 73.14    a 33.71 73.10 0.24331050        6
#>   3:  3 33.56 72.91    a 33.71 73.10 0.24207437        9
#>   4:  4 33.52 73.14    a 33.71 73.10 0.19416488        6
#>   5:  5 33.54 72.97    a 33.71 73.10 0.21400935        9
#>   6:  6 33.62 73.02    a 33.71 73.10 0.12041595        5
#>   7:  7 33.70 72.98    a 33.71 73.10 0.12041595        3
#>   8:  8 33.67 73.12    a 33.71 73.10 0.04472136        2
#>   9:  9 33.57 73.07    a 33.71 73.10 0.14317821        7
#>  10: 10 33.58 73.16    a 33.71 73.10 0.14317821        6
#>  11:  1 33.65 73.10    b 33.67 73.14 0.04472136        2
#>  12:  2 33.47 73.14    b 33.67 73.14 0.20000000        5
#>  13:  3 33.56 72.91    b 33.67 73.14 0.25495098       10
#>  14:  4 33.52 73.14    b 33.67 73.14 0.15000000        5
#>  15:  5 33.54 72.97    b 33.67 73.14 0.21400935       10
#>  16:  6 33.62 73.02    b 33.67 73.14 0.13000000        9
#>  17:  7 33.70 72.98    b 33.67 73.14 0.16278821        8
#>  18:  8 33.67 73.12    b 33.67 73.14 0.02000000        1
#>  19:  9 33.57 73.07    b 33.67 73.14 0.12206556        5
#>  20: 10 33.58 73.16    b 33.67 73.14 0.09219544        4
#>  21:  1 33.65 73.10    c 33.61 73.10 0.04000000        1
#>  22:  2 33.47 73.14    c 33.61 73.10 0.14560220        3
#>  23:  3 33.56 72.91    c 33.61 73.10 0.19646883        7
#>  24:  4 33.52 73.14    c 33.61 73.10 0.09848858        3
#>  25:  5 33.54 72.97    c 33.61 73.10 0.14764823        7
#>  26:  6 33.62 73.02    c 33.61 73.10 0.08062258        4
#>  27:  7 33.70 72.98    c 33.61 73.10 0.15000000        6
#>  28:  8 33.67 73.12    c 33.61 73.10 0.06324555        3
#>  29:  9 33.57 73.07    c 33.61 73.10 0.05000000        3
#>  30: 10 33.58 73.16    c 33.61 73.10 0.06708204        2
#>  31:  1 33.65 73.10    d 33.58 73.07 0.07615773        5
#>  32:  2 33.47 73.14    d 33.58 73.07 0.13038405        2
#>  33:  3 33.56 72.91    d 33.58 73.07 0.16124515        5
#>  34:  4 33.52 73.14    d 33.58 73.07 0.09219544        2
#>  35:  5 33.54 72.97    d 33.58 73.07 0.10770330        4
#>  36:  6 33.62 73.02    d 33.58 73.07 0.06403124        2
#>  37:  7 33.70 72.98    d 33.58 73.07 0.15000000        7
#>  38:  8 33.67 73.12    d 33.58 73.07 0.10295630        6
#>  39:  9 33.57 73.07    d 33.58 73.07 0.01000000        1
#>  40: 10 33.58 73.16    d 33.58 73.07 0.09000000        3
#>  41:  1 33.65 73.10    e 33.61 73.07 0.05000000        3
#>  42:  2 33.47 73.14    e 33.61 73.07 0.15652476        4
#>  43:  3 33.56 72.91    e 33.61 73.07 0.16763055        6
#>  44:  4 33.52 73.14    e 33.61 73.07 0.11401754        4
#>  45:  5 33.54 72.97    e 33.61 73.07 0.12206556        5
#>  46:  6 33.62 73.02    e 33.61 73.07 0.05099020        1
#>  47:  7 33.70 72.98    e 33.61 73.07 0.12727922        4
#>  48:  8 33.67 73.12    e 33.61 73.07 0.07810250        4
#>  49:  9 33.57 73.07    e 33.61 73.07 0.04000000        2
#>  50: 10 33.58 73.16    e 33.61 73.07 0.09486833        5
#>  51:  1 33.65 73.10    f 33.67 72.91 0.19104973        8
#>  52:  2 33.47 73.14    f 33.67 72.91 0.30479501       10
#>  53:  3 33.56 72.91    f 33.67 72.91 0.11000000        4
#>  54:  4 33.52 73.14    f 33.67 72.91 0.27459060       10
#>  55:  5 33.54 72.97    f 33.67 72.91 0.14317821        6
#>  56:  6 33.62 73.02    f 33.67 72.91 0.12083046        6
#>  57:  7 33.70 72.98    f 33.67 72.91 0.07615773        2
#>  58:  8 33.67 73.12    f 33.67 72.91 0.21000000        8
#>  59:  9 33.57 73.07    f 33.67 72.91 0.18867962        9
#>  60: 10 33.58 73.16    f 33.67 72.91 0.26570661        9
#>  61:  1 33.65 73.10    g 33.59 72.90 0.20880613        9
#>  62:  2 33.47 73.14    g 33.59 72.90 0.26832816        8
#>  63:  3 33.56 72.91    g 33.59 72.90 0.03162278        1
#>  64:  4 33.52 73.14    g 33.59 72.90 0.25000000        8
#>  65:  5 33.54 72.97    g 33.59 72.90 0.08602325        1
#>  66:  6 33.62 73.02    g 33.59 72.90 0.12369317        7
#>  67:  7 33.70 72.98    g 33.59 72.90 0.13601471        5
#>  68:  8 33.67 73.12    g 33.59 72.90 0.23409400        9
#>  69:  9 33.57 73.07    g 33.59 72.90 0.17117243        8
#>  70: 10 33.58 73.16    g 33.59 72.90 0.26019224        8
#>  71:  1 33.65 73.10    h 33.57 73.14 0.08944272        6
#>  72:  2 33.47 73.14    h 33.57 73.14 0.10000000        1
#>  73:  3 33.56 72.91    h 33.57 73.14 0.23021729        8
#>  74:  4 33.52 73.14    h 33.57 73.14 0.05000000        1
#>  75:  5 33.54 72.97    h 33.57 73.14 0.17262677        8
#>  76:  6 33.62 73.02    h 33.57 73.14 0.13000000        8
#>  77:  7 33.70 72.98    h 33.57 73.14 0.20615528       10
#>  78:  8 33.67 73.12    h 33.57 73.14 0.10198039        5
#>  79:  9 33.57 73.07    h 33.57 73.14 0.07000000        4
#>  80: 10 33.58 73.16    h 33.57 73.14 0.02236068        1
#>  81:  1 33.65 73.10    i 33.64 72.95 0.15033296        7
#>  82:  2 33.47 73.14    i 33.64 72.95 0.25495098        7
#>  83:  3 33.56 72.91    i 33.64 72.95 0.08944272        3
#>  84:  4 33.52 73.14    i 33.64 72.95 0.22472205        7
#>  85:  5 33.54 72.97    i 33.64 72.95 0.10198039        3
#>  86:  6 33.62 73.02    i 33.64 72.95 0.07280110        3
#>  87:  7 33.70 72.98    i 33.64 72.95 0.06708204        1
#>  88:  8 33.67 73.12    i 33.64 72.95 0.17262677        7
#>  89:  9 33.57 73.07    i 33.64 72.95 0.13892444        6
#>  90: 10 33.58 73.16    i 33.64 72.95 0.21840330        7
#>  91:  1 33.65 73.10    j 33.54 72.88 0.24596748       10
#>  92:  2 33.47 73.14    j 33.54 72.88 0.26925824        9
#>  93:  3 33.56 72.91    j 33.54 72.88 0.03605551        2
#>  94:  4 33.52 73.14    j 33.54 72.88 0.26076810        9
#>  95:  5 33.54 72.97    j 33.54 72.88 0.09000000        2
#>  96:  6 33.62 73.02    j 33.54 72.88 0.16124515       10
#>  97:  7 33.70 72.98    j 33.54 72.88 0.18867962        9
#>  98:  8 33.67 73.12    j 33.54 72.88 0.27294688       10
#>  99:  9 33.57 73.07    j 33.54 72.88 0.19235384       10
#> 100: 10 33.58 73.16    j 33.54 72.88 0.28284271       10
#>      ID   lat   lon i.ID i.lat i.lon     EuDist distRank
    #selecting the shortest distance
    df1<-df1[distRank==1]
    for (j in 1:10) { 
      df1[j]
      print(df1[j])
      #Sys.sleep(60)
    }
#>    ID   lat   lon i.ID i.lat i.lon EuDist distRank
#> 1:  8 33.67 73.12    b 33.67 73.14   0.02        1
#>    ID   lat  lon i.ID i.lat i.lon EuDist distRank
#> 1:  1 33.65 73.1    c 33.61  73.1   0.04        1
#>    ID   lat   lon i.ID i.lat i.lon EuDist distRank
#> 1:  9 33.57 73.07    d 33.58 73.07   0.01        1
#>    ID   lat   lon i.ID i.lat i.lon    EuDist distRank
#> 1:  6 33.62 73.02    e 33.61 73.07 0.0509902        1
#>    ID   lat   lon i.ID i.lat i.lon     EuDist distRank
#> 1:  3 33.56 72.91    g 33.59  72.9 0.03162278        1
#>    ID   lat   lon i.ID i.lat i.lon     EuDist distRank
#> 1:  5 33.54 72.97    g 33.59  72.9 0.08602325        1
#>    ID   lat   lon i.ID i.lat i.lon EuDist distRank
#> 1:  2 33.47 73.14    h 33.57 73.14    0.1        1
#>    ID   lat   lon i.ID i.lat i.lon EuDist distRank
#> 1:  4 33.52 73.14    h 33.57 73.14   0.05        1
#>    ID   lat   lon i.ID i.lat i.lon     EuDist distRank
#> 1: 10 33.58 73.16    h 33.57 73.14 0.02236068        1
#>    ID  lat   lon i.ID i.lat i.lon     EuDist distRank
#> 1:  7 33.7 72.98    i 33.64 72.95 0.06708204        1
    df1$lat
#>  [1] 33.67 33.65 33.57 33.62 33.56 33.54 33.47 33.52 33.58 33.70
    df1$lon
#>  [1] 73.12 73.10 73.07 73.02 72.91 72.97 73.14 73.14 73.16 72.98
    df1$i.lat
#>  [1] 33.67 33.61 33.58 33.61 **33.59 33.59 33.57 33.57 33.57** 33.64
    df1$i.lon
#>  [1] 73.14 73.10 73.07 73.07 **72.90 72.90 73.14 73.14 73.14** 72.95

Created on 2020-08-22 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.