Mapping (x, y) coordinates to nearest point of a set in R

风流意气都作罢 提交于 2019-12-11 05:57:16

问题


I am building a shiny application, and I have a line of code that is currently slowing me down quite a bit. I have the following dataframe, with 1008 unique (x,y) coordinates (apologies for the large copy and paste, although I think sharing this whole dataframe is helpful):

dput(rounded_coords)
structure(list(xspots = c(1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 
14.5, 16, 17.5, 19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 
34, 35.5, 37, 38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 
4.75, 6.25, 7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 
19.75, 21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 
33.25, 34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 
46.75, 48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 
17.5, 19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 
37, 38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 
6.25, 7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49, 1.75, 3.25, 4.75, 6.25, 
7.75, 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 
21.25, 22.75, 24.25, 25.75, 27.25, 28.75, 30.25, 31.75, 33.25, 
34.75, 36.25, 37.75, 39.25, 40.75, 42.25, 43.75, 45.25, 46.75, 
48.25, 1, 2.5, 4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5, 16, 17.5, 
19, 20.5, 22, 23.5, 25, 26.5, 28, 29.5, 31, 32.5, 34, 35.5, 37, 
38.5, 40, 41.5, 43, 44.5, 46, 47.5, 49), yspots = c(1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 
5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 
5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8.5, 8.5, 8.5, 8.5, 8.5, 
8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 
8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 
8.5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 
11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 
11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 
11.5, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 
13, 13, 13, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 
14.5, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
16, 16, 16, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 
17.5, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
19, 19, 19, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 
20.5, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
22, 22, 22, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 
23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 
23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 
23.5, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 
25, 25, 25, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 
26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 
26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 26.5, 
26.5, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 
28, 28, 28, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 
29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 
29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 29.5, 
29.5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 
31, 31, 31, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 
32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 
32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 32.5, 
32.5, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 
34, 34, 34, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 
35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 
35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 35.5, 
35.5, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 
37, 37, 37, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 
38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 
38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 38.5, 
38.5, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 
40, 40, 40, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 
41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 
41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 
41.5, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 
43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 
43, 43, 43, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 
44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 
44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 
44.5, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 
46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 
46, 46, 46)), .Names = c("xspots", "yspots"), row.names = c(NA, 
-1008L), class = "data.frame")

I have a second dataframe as well, unrounded_coords, and I would like to round the values in unrounded_coords to its nearest value in rounded_coords. A subset of my dataframe unrounded_coords is shared below:

dput(unrounded_coords[1:100, ])
structure(list(xmapped = c(24.9, 23.2, 3.6, 11.9, 26, 29.7, 46.7, 
13.3, 23.9, 24.5, 24.1, 34.6, 24.3, 39.8, 35.9, 25.2, 19.7, 36.7, 
3.2, 24.5, 34.2, 36.2, 24.4, 10.5, 31.5, 25.8, 31.4, 18, 25.3, 
25, 25.9, 24.5, 24.8, 8.6, 5.8, 23.4, 48.2, 38.5, 24, 9, 30.4, 
8.2, 32, 22.3, 24.9, 41.9, 28.1, 26.3, 34.8, 19.8, 2.2, 25.8, 
14.3, 25.3, 6.3, 25.3, 23.8, 5.1, 6.7, 24.8, 24.2, 25.6, 47.2, 
25.3, 25, 39.1, 48.9, 23.9, 40, 24.1, 12.4, 12.5, 5.4, 32, 14.3, 
26.4, 41.2, 32.7, 25.2, 23.7, 38.1, 25.6, 31.9, 43.3, 22.2, 27.9, 
34.6, 41.6, 36.8, 26.3, 25.9, 25, 24.4, 24.4, 27, 29.1, 24.8, 
4, 18, 27.4), ymapped = c(31.9, 41.5, 28.1, 33.6, 39.6, 22.9, 
36.3, 38.4, 41.3, 40.8, 32.8, 17.4, 38.8, 40.4, 29.8, 40.3, 16.1, 
19.7, 27.8, 40.6, 27.6, 26.9, 37.5, 25.7, 27.7, 41.3, 18.2, 17.6, 
39.7, 42.5, 40.8, 40.9, 41.8, 21.7, 32.7, 40.2, 39.2, 19.8, 41.9, 
21.9, 36.9, 39.8, 35.6, 31.4, 33.4, 35, 17.1, 40.2, 30.4, 29.1, 
42, 41.8, 18.6, 41.1, 23.6, 41, 39.7, 25.4, 22, 41.4, 40.4, 40.7, 
42.2, 41.1, 42.5, 30.4, 35.8, 40.4, 19.4, 38.4, 19.9, 38.1, 19.2, 
35.1, 17.7, 41.6, 34.6, 17.1, 40.3, 40.5, 30.2, 24.5, 27.3, 38.7, 
32.9, 18.5, 26.2, 33.6, 39, 39.8, 40.6, 30.6, 33.6, 39.8, 28.9, 
17.5, 41.1, 25.7, 40.2, 39.9)), .Names = c("xmapped", "ymapped"
), row.names = c(NA, 100L), class = "data.frame")

Currently I use the following code to map the values in unrounded_coords to their nearest value in rounded_coords:

output.df <- apply(unrounded_coords, 1, FUN = function(x) {
    zed <- sqrt((x[1] - rounded_coords$xspots)^2 + (x[2] - rounded_coords$yspots)^2)
    return(c(rounded_coords$xspots[which.min(zed)], rounded_coords$yspots[which.min(zed)]))
  }) %>%
    t() %>%
    as.data.frame() %>%
    setNames(c('xrounded', 'yrounded')) 

Unfortunately, this function on my entire dataframe takes too long to run (I am using this in a Shiny application and this function has to run somewhat frequently). Any thoughts on how I could speed this up are greatly appreciated.

EDIT - the reason the code takes quite long is because, for each point in unrounded_coords (my dataframe has 40,000 rows, and this dataframe grows daily), it needs to compute the distance with each of ~1,000 points in rounded_coords. That is 40,000,000 computations, and I'm not sure how to get this number lower.

EDIT2 - the column names xmapped and ymapped in the unrounded_coords dataframe may be misleading. These are obviously the unmapped values, and the column names should probably be xraw, yraw, or something like that.


回答1:


Not sure if this will be any faster but you can give it a go.

library(spatstat)

# this calculates a distance matrix
# each value in rounded values has every distance to a point in unrounded as a row
distances <- crossdist(X = rounded_coords[, 1], 
                      Y = rounded_coords[, 2],
                      x2 = unrounded_coords[, 1],
                      y2 = unrounded_coords[, 2])

# if each column is an unrounded, you want the column min position
min_locations <- apply(distances, 2, which.min)

# then subset rounded_coords to get the coordinate that are closest
mapped_coords <- rounded_coords[min_locations, ]
names(mapped_coords) <- c('xrounded', 'yrounded')


来源:https://stackoverflow.com/questions/47382708/mapping-x-y-coordinates-to-nearest-point-of-a-set-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!