拆分-应用-与dist_google()函数(stplanr程序包)结合使用

时间:2019-01-04 19:06:43

标签: r google-distancematrix-api split-apply-combine

我有以下经/纬点(points)数据框:

 GPSLatitude GPSLongitude
1     40.66126     22.89565
2     40.66127     22.89565
3     40.66128     22.89565
4     40.66130     22.89566
5     40.66131     22.89567
6     40.66132     22.89569
7     40.66134     22.89573
8     40.66136     22.89577
9     40.66137     22.89582
10    40.66141     22.89594
11    40.66142     22.89601
12    40.66145     22.89609
13    40.66147     22.89618
14    40.66150     22.89627
15    40.66152     22.89635
16    40.66155     22.89644
17    40.66160     22.89650
18    40.66165     22.89654
19    40.66172     22.89656
20    40.66178     22.89658
21    40.66186     22.89659
22    40.66193     22.89660
23    40.66200     22.89662
24    40.66207     22.89663
25    40.66213     22.89664
26    40.66218     22.89665
27    40.66223     22.89665
28    40.66227     22.89664
29    40.66230     22.89663
30    40.66234     22.89662
31    40.66238     22.89661
32    40.66242     22.89662
33    40.66244     22.89664
34    40.66245     22.89666
35    40.66247     22.89669
36    40.66248     22.89671
37    40.66249     22.89673
38    40.66250     22.89674
39    40.66251     22.89676
40    40.66253     22.89679
41    40.66255     22.89683
42    40.66257     22.89686
43    40.66261     22.89694
44    40.66263     22.89698
45    40.66265     22.89700
46    40.66267     22.89702
47    40.66268     22.89705
48    40.66270     22.89707
49    40.66272     22.89709

和以下参考点(point17):

22.89704,40.66265

我想拆分-每25行应用dist_google(),因为dist_google函数最多可以应用25对源-目标。最后,我想将结果合并到一个新的数据框中。我试图用一个简单的for loop来做到这一点:

for (i in 1:nrow(points)) {
myresults[i,]<- dist_google(from = point17, to = points[i,],  mode = "driving", google_api = "my_api_key")

,但是需要花费一些时间来计算结果。因此,我决定每25行拆分一次数据帧并应用该函数(此解决方案可立即计算结果)。我已经尝试过adply()软件包的plyr函数:

adply(points17, 25, dist_google(from = pointLC17, to = points17[i,],  mode = "driving", google_api = "my_api_key"))

但是adply()的.margins参数出错。有解决这个问题的想法吗? 预先谢谢你!

1 个答案:

答案 0 :(得分:0)

我不了解25个小组的逻辑,但是您可以这样做:

# dt is your data frame (points)

# create groups of 25 
groups <- split(d, ceiling(seq_along(nrow(dt))/25))

# here the calculated distance will be stored
new_df <- vector(mode = 'numeric', length = nrow(dt))

for(gr in groups){

    for(i in gr){

        val <- stplanr::dist_google(from = ref, to = as.numeric(dt[i,]), api_key)
        new_df[i] <- val
    }

}