I have two data.table
s. They contain x & y coordinates for some points. They are as following:-
set.seed(123)
a <- data.table(x = runif(10), y = runif(10))
a
x y
1: 0.9036218 0.005233401
2: 0.1914482 0.219560561
3: 0.6089018 0.275966545
4: 0.2307832 0.665939467
5: 0.5959250 0.221657568
6: 0.2343375 0.228499024
7: 0.2620843 0.846292513
8: 0.8499895 0.888969644
9: 0.5707878 0.305334950
10: 0.4771339 0.714933915
b <- data.table(x = runif(30), y = runif(30))
b
x y
1: 0.86444548 0.26182116
2: 0.54160133 0.14384453
3: 0.84231617 0.78842707
4: 0.26452802 0.56285334
5: 0.31283576 0.63412792
6: 0.85437689 0.91124597
7: 0.71066455 0.94311426
8: 0.28285278 0.36521536
9: 0.28785318 0.95902709
10: 0.86013854 0.09544158
11: 0.64630209 0.49041844
12: 0.54615120 0.35911319
13: 0.15450425 0.13447951
14: 0.49790235 0.17264283
15: 0.08562707 0.74520138
16: 0.93701477 0.16472997
17: 0.62143442 0.36910724
18: 0.30887985 0.75412718
19: 0.64023414 0.14568618
20: 0.81968754 0.34259618
21: 0.77749145 0.20355856
22: 0.55769767 0.44691877
23: 0.39907966 0.26292129
24: 0.07789015 0.96241503
25: 0.13784013 0.33544759
26: 0.99523977 0.38647609
27: 0.49555325 0.97433126
28: 0.34634466 0.99967023
29: 0.08722789 0.56224314
30: 0.27777771 0.30246561
For each point in a
, I want to find nearest point to them in b
. I.E., each point in a
will have a nearest point from b
. And the coordinates of the nearest point should be joined with a
.
How can I do this?
Thanks in advance
set.seed(123)
at the top, to make your data reproducible. – Basswood