【问题标题】:String causes geocode to throw error字符串导致地理编码抛出错误
【发布时间】:2018-07-23 07:45:47
【问题描述】:

运行这行代码

library(ggmap)
geocode(location="Somewhere in Nigeria Winning",source="dsk", output="more")

抛出此错误

Error in vapply(gc$results[[1]]$address_components, function(x) x[[nameToGrab]],  : values must be length 1, but FUN(X[[1]]) result is length 0

有人可以解释为什么会发生这种情况以及我可以做些什么来解决它?我知道这似乎是一个奇怪的问题,但是这个字符串出现在一组要修剪的数据中,并且似乎破坏了地理编码。

【问题讨论】:

  • 可能是因为找不到“Somewhere in Nigeria Winning”作为位置
  • 由于某种原因,添加了output = "more" 为这个特定的查询带来了问题。 latlonlatlonaall 按预期工作。
  • Data Science Toolkit API 宕机了一段时间(我认为它受到了轰炸),自从它回来后,它的行为就有些不同了。我不确定这就是这里发生的事情——nielfws 是对的; output = "more 似乎有问题——但它可能是问题的根源。
  • 如果您尝试从字符串中解析国家/地区,I wrote a function for that 提供了一些选项。
  • 如果找不到它,它会返回一个零结果字符串,但是该特定字符串似乎会将代码踢到错误区域,必须使用所有有点夸大我的数据时间,但我会试一试。跨度>

标签: r ggmap geocode


【解决方案1】:

我只是想发布一个答案,因为我已经找到了问题,并且任何人都想要解决方法/想知道该怎么做。问题不在于 DSK,而在于 ggmap 代码本身。如果你跑

geocode(location="not a place in the slightest",source="dsk", output="more")

然后库将优雅地失败并给你预期的回报。

geocode failed with status ZERO_RESULTS, location = "not a place in the slightest" 

但是,由于 DSK 返回数据但它不是好的数据,因此中间的某些字符串(如上面的字符串)失败得不太优雅使用你需要使用 all 的 more 标志来获得它。

$results[[1]]$address_components[[1]]$long_name
NULL

$results[[1]]$address_components[[1]]$short_name
NULL

lib 中的代码将其解析为带有“more”标志的数据帧

nameToGrab   <- `if`(nameType == "long", "long_name", "short_name")
outputVals  <- vapply(gc$results[[1]]$address_components, function(x)  
x[[nameToGrab]], character(1))
outputNames <- vapply(gc$results[[1]]$address_components, function(x){ if(length(x$types) == 0) return("query")
      x$types[1]
    },
    character(1)
)

因为 long_name 和 short_name 都是 NULL,所以 vapply 将会失败。

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2018-04-06
    • 2012-03-06
    • 2021-05-13
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多