Let's say I have 2 data tables with arbitrary positions and scores. All positions in each data table are unique. For example:
dt1:
position score
10 6.2
21 4.5
37 3.6
dt2:
position score
8 12.2
32 4.2
45 3.8
52 4.9
And I want to get position in df2 that is the closest to each row in df1. So for example, position 10 in df1 has df2 of position 8 as the closest. In the end the result will look like this:
position.dt1 score.dt1 position.dt2 score.dt2 distance
10 6.2 8 12.2 2
21 4.5 32 4.2 11
37 3.6 32 4.2 5
How to achieve this in R?