将(x,y)坐标映射到R中集合的最近点 [英] Mapping (x, y) coordinates to nearest point of a set in R

查看:43
本文介绍了将(x,y)坐标映射到R中集合的最近点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个有光泽的应用程序,并且我有一行代码目前正在使我慢下来.我有以下具有1008个唯一(x,y)坐标的数据框(为大拷贝粘贴而道歉,尽管我认为共享整个数据框是有帮助的):

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")

我还有第二个数据框 unrounded_coords ,我想将 unrounded_coords 中的值四舍五入到 rounded_coords 中最接近的值..我的数据框 unrounded_coords 的一部分在下面共享:

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")

当前,我使用以下代码将 unrounded_coords 中的值映射到它们在 rounded_coords 中最接近的值:

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')) 

不幸的是,此函数在我的整个数据帧上花费的时间太长(我在Shiny应用程序中使用了此函数,并且该函数必须经常运行).非常感谢我对如何加快速度的任何想法.

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.

编辑-代码花费很长时间的原因是,对于unrounded_coords中的每个点(我的数据框有40,000行,并且该数据框每天都在增长),它需要计算rounded_coords中每〜1,000点的距离.那是40,000,000次计算,我不确定如何降低这个数字.

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-unrounded_coords数据框中的xmapped和ymapped列名称可能会引起误解.这些显然是未映射的值,列名称可能应该是xraw,yraw或类似的名称.

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.

推荐答案

不确定是否会更快,但是您可以尝试一下.

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')

这篇关于将(x,y)坐标映射到R中集合的最近点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆