【问题标题】:geocode adresses in R: grep() lookup world.cities datasetR 中的地理编码地址:grep() 查找 world.cities 数据集
【发布时间】:2014-09-12 13:36:58
【问题描述】:

我正在尝试对存储在字符向量中的一些地址进行地理编码。我在ggmaps 中使用了geocode() 函数;但是,它只分类了我大约 50% 的地址。我希望使用更基本的方法来查找城市名称(来自maps 包中的world.cities 数据是否在我的地址列表中,如果是,则从该查找表中获取经度和纬度信息。我将尝试清理返回的文件,并用 R 提供的其他地理编码方法(对各种外部 API 的调用)对其进行补充。到目前为止我编码的内容如下:

places <- c("Atlanta,Georgia", "My house, Paris, France", "Some Other House, Paris, Ontario, Canada", "Paris", "Oxford", "Oxford, USA")

library(maps)
data(world.cities)
ddd <- world.cities[world.cities$name %in% c("Paris","Oxford","New York"),]

is.integer0 <- function(x) {
is.integer(x) && length(x) == 0L
}

for (i in 1:length(places)) {
  for (j in 1:dim(ddd)[1]) {
  k <- ddd$name[j]
    if (is.integer0(grep(k,places[i],perl=TRUE))==TRUE) next
      if (exists("zzz")==FALSE) {
        zzz <- cbind(places[i],ddd[j,1:5])
      } else {
        zzz <- rbind(zzz,cbind(places[i],ddd[j,1:5])) 
      } 
  }
}

输出是我想要的(稍后我会主观地清理它)。我的问题是我的真实数据大约是 8000 个地址,world.cities 数据大约是 40000 多个城市,所以双 for 循环方法有点慢。与 R 中的其他任务一样,我想这可以通过 apply 系列的某些成员进行矢量化。我很难弄清楚如何去做。有什么想法吗?

### Output
                                      places[i]   name country.etc     pop    lat  long
28245                   My house, Paris, France  Paris      Canada   10570  43.20  0.38
28246                   My house, Paris, France  Paris      France 2141839  48.86   2.34
282451 Some Other House, Paris, Ontario, Canada  Paris      Canada   10570  43.20 -80.38
282461 Some Other House, Paris, Ontario, Canada  Paris      France 2141839  48.86   2.34
282452                                    Paris  Paris      Canada   10570  43.20 -80.38
282462                                    Paris  Paris      France 2141839  48.86   2.34
27671                                    Oxford Oxford      Canada    1271  45.73 -63.87
27672                                    Oxford Oxford New Zealand    1816 -43.30 172.18
27673                                    Oxford Oxford          UK  157568  51.76  -1.26
276711                              Oxford, USA Oxford      Canada    1271  45.73 -63.87
276721                              Oxford, USA Oxford New Zealand    1816 -43.30 172.18
276731                              Oxford, USA Oxford          UK  157568  51.76  -1.26

经过进一步的数据清理后,我真的很想要:

### Output
                                      places[i]   name country.etc     pop    lat  long
 28246                   My house, Paris, France  Paris      France 2141839  48.86   2.34
282451 Some Other House, Paris, Ontario, Canada  Paris      Canada   10570  43.20 -80.38
282462                                    Paris  Paris      France 2141839  48.86   2.34
27673                                    Oxford Oxford          UK  157568  51.76  -1.26
276731                              Oxford, USA Oxford          NA       NA    NA  NA
                               Atlanta, Georgia     NA          NA       NA    NA  NA

基本上,逻辑是:

  1. 如果国家/地区也匹配地点字符串,则保留该行。例如,法国巴黎和加拿大巴黎。
  2. 如果 places 字符串包含一个单词,则猜测它们指的是人口最多的城市。所以默认 Paris 为 Paris, France 和 Oxford 为 Oxford UK。因为很难对非唯一地址进行地理编码。
  3. 如果 places 字符串包含多个单词但 Country 不匹配任何其他单词,例如 Oxford, USA。然后制作除城市 NA 之外的所有内容。在这里,我将尝试使用geocode() 和其他服务来获取更好的信息。
  4. 如果地点地址从未在查找字典中添加,然后尝试使用geocode() 等填写所有内容(真的我只想要长/纬度)。这就是佐治亚州亚特兰大的示例。

对一般方法的想法以及如何在 R 中做得更好?如上所述,这种方法的推动力是看看我是否可以补充我已经得到的(使用geocode() 函数的 50% 地理编码地址)

【问题讨论】:

    标签: regex r geocode


    【解决方案1】:

    这使得城市提取更加通用(使用字符串正则表达式匹配),然后与world.cities 数据进行合并:

    places_dat <- cbind(places, Reduce(rbind, 
                    lapply(str_match_all(places, ",*\ *([[:alpha:]]+)\ *,\ *([[:alpha:]]+)\ *$"),
                      function(x) {
    
      if (length(x) == 0) {
        return(data.frame(city=NA, state=NA))
      } else {
        return(data.frame(city=x[,2], state=x[,3]))
      }
    
    })))
    
    places_dat
    
    ##                                     places    city   state
    ## 1                          Atlanta,Georgia Atlanta Georgia
    ## 2                  My house, Paris, France   Paris  France
    ## 3 Some Other House, Paris, Ontario, Canada Ontario  Canada
    ## 4                                    Paris    <NA>    <NA>
    ## 5                                   Oxford    <NA>    <NA>
    ## 6                              Oxford, USA  Oxford     USA
    ## 
    
    merge(places_dat, world.cities, by.x="city", by.y="name", all.x=TRUE)
    
    ##      city                                   places   state country.etc     pop    lat    long capital
    ## 1 Atlanta                          Atlanta,Georgia Georgia         USA  424096  33.76  -84.42       0
    ## 2   Paris                  My house, Paris, France  France      France 2141839  48.86    2.34       1
    ## 3   Paris                  My house, Paris, France  France      Canada   10570  43.20  -80.38       0
    ## 4 Ontario Some Other House, Paris, Ontario, Canada  Canada         USA  175805  34.05 -117.61       0
    ## 5  Oxford                              Oxford, USA     USA      Canada    1271  45.73  -63.87       0
    ## 6  Oxford                              Oxford, USA     USA New Zealand    1816 -43.30  172.18       0
    ## 7  Oxford                              Oxford, USA     USA          UK  157568  51.76   -1.26       0
    ## 8    <NA>                                    Paris    <NA>        <NA>      NA     NA      NA      NA
    ## 9    <NA>                                   Oxford    <NA>        <NA>      NA     NA      NA      NA
    

    它仍然需要一些筛选(也许complete.cases 作为一个步骤),但它会让你走得更远,应该更快一些。

    【讨论】:

    • lapply() 示例和正则表达式预清理是有用的提示!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2011-03-18
    • 1970-01-01
    • 2018-10-22
    • 2021-07-19
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多