【问题标题】:How can I extract the zip code from the ggmap output?如何从 ggmap 输出中提取邮政编码?
【发布时间】:2021-02-24 20:22:16
【问题描述】:
structure(list(trip_count = 1:10, pickup_longitude = c(-73.964096, 
-73.989037, -73.934998, -73.93409, -73.998222, -74.004478, -73.994881, 
-73.955917, -73.993607, -73.948265), pickup_latitude = c(40.764141, 
40.760208, 40.746693, 40.715908, 40.750809, 40.741501, 40.74033, 
40.776054, 40.758625, 40.778515)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

所以我正在尝试将经度和纬度数据重新编码为纽约市的邮政编码。 使用revgeocodingfrom ggmap 我已经得到了一些结果:

res <- revgeocode(c(test$pickup_longitude[1],test$pickup_latitude[1]), output = "all")

然而,

  1. 我可以一次只将此过程应用于一个位置数据。有没有一种方法可以一次获得每个位置的结果,例如
res <- revgeocode(c(test$pickup_longitude,test$pickup_latitude), output = "all")
  1. 如何从 ggmap 结果中提取邮政编码,以便在数据框中使用对应的邮政编码创建一个新列?不知何故,邮政编码以一种非常奇怪的方式存储(见图)。如何在控制台中访问这些信息?

解决方案不必在ggmap 中工作,也许有不同的方法可以从经度和纬度数据中获取邮政编码? 谢谢!

【问题讨论】:

  • 你能显示来自revgeocode 的示例输出吗?我没有有效的 API 密钥。
  • sapply(data.frame(t(df[2:3])), revgeocode, output='all') 应该可以工作。虽然可能revgeocode 确实需要一个矩阵

标签: r geocoding ggmap


【解决方案1】:

我没有 API 密钥,但请尝试以下操作:

my_func <- function(longlat, output){
  list(list(list(c(vector("list", 7), list(12345L)))))
}

test %>% 
  rowwise() %>% 
  mutate(zip = list(my_func(c(pickup_longitude, pickup_latitude), output = "all"))) %>% 
  mutate(zip = zip[[1]][[1]],
         zip = zip[[8]])

my_func 替换为revgeocode 。您必须弄清楚如何从输出中找出邮政编码。但是你可以试试上面的方法。

【讨论】:

  • 谢谢迈克尔,不幸的是我得到:错误:mutate() 输入问题zip。 x is.numeric(location) && length(location) == 2 is not TRUE ℹ 输入 ziprevgeocode(pickup_longitude, pickup_latitude, output = "all")。 ℹ 错误发生在第 1 行。
  • 显然,一次只能获取一个邮政编码
  • 对不起,我没看懂revgeocode的格式。试试我更新的答案。
  • Google API 是否一次只允许一个邮政编码并不重要。您的脚本可以为您处理迭代。
  • 嘿迈克尔,效果很好!您只需在my_func 之前添加list(),但是,这并不能解决我的第二个问题,因为它会为每个数据点返回一个列表。我只需要 zip,但我不知道它是如何存储在 revgeocode 中的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
相关资源
最近更新 更多