如何从ggmap输出中提取邮政编码?

问题描述

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

因此,我试图将经度和纬度数据重新编码为NYC的邮政编码。 使用revgeocoding中的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结果中提取邮政编码,以便可以使用相应的邮政编码在数据框中创建一个新列?邮政编码以某种奇怪的方式存储(参见图片)。如何在控制台中访问此信息?

    enter image description here

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

解决方法

我没有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 。您将必须弄清楚如何从输出中选择邮政编码。但是您可以尝试上述类似方法。