我正在构建一个闪亮的应用程序,我有一行代码,目前正在减慢我的速度。我有以下数据框,有1008个唯一(x,y)坐标(对于大型复制和粘贴的道歉,虽然我认为共享整个数据框是有帮助的):
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")
我还有第二个数据框, unrounded_coords ,我想将 unrounded_coords 中的值四舍五入到 rounded_coords 中最接近的值。我的数据框 unrounded_coords 的子集在下面共享:
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")
目前,我使用以下代码将 unrounded_coords 中的值映射到 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'))
不幸的是,我的整个数据帧上的这个函数运行时间太长(我在Shiny应用程序中使用它并且此函数必须经常运行)。任何关于如何加快速度的想法都非常感激。
编辑 - 代码耗时很长的原因是,对于unrounded_coords中的每个点(我的数据帧有40,000行,并且此数据帧每天增长),它需要计算round_coords中每个~1,000个点的距离。这是40,000,000次计算,我不知道如何降低这个数字。
EDIT2 - unrounded_coords数据框中的列名xmapped和ymapped可能会产生误导。这些显然是未映射的值,列名应该是xraw,yraw或类似的东西。
答案 0 :(得分:2)
不确定这是否会更快,但你可以试一试。
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')