【问题标题】:Skip the error when calculating travel distances using for loop and georoute使用 for loop 和 georoute 计算行驶距离时跳过错误
【发布时间】:2015-10-29 21:21:57
【问题描述】:

我在 R 中使用 taRifx.geo 包来计算到地址之间的行驶距离。我正在做的是将起点和终点放在数据框的两列中。据我了解,georoute 一次只能计算一对距离,所以我编写了一个 for 循环来计算数据框中的所有对。但是当出现错误时循环停止。

我想要的不是停止循环,而是在遇到错误并从下一对继续时将值 NA 值放入距离向量中。我知道我应该使用一些东西作为 tryCatch 但我不知道如何在这里编写代码。任何人都可以帮忙吗?非常感谢!

library(taRifx.geo)

distance=data.frame(matrix(0,200,1))

for(i in 1:200){

  distance[i,1]=as.matrix(georoute( c(as.character(address$capitol[i]),
                          as.character(address$capitol_destination[i])),
                        verbose=TRUE, returntype="distance",
                        service="bing" ))

}

【问题讨论】:

  • 谢谢 Mauricio,我会试试你的代码!

标签: r loops error-handling geocoding


【解决方案1】:
tryCatch(as.matrix(georoute( c(as.character(address$capitol[i]),
                           as.character(address$capitol_destination[i])),
                           verbose=TRUE, returntype="distance",
                           service="bing" )),  error=function(a) {"."} )

【讨论】:

  • 虽然代码很好,但如果你能解释一下你的解决方案是如何工作的,那就更好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多