仅从地理编码中抽取邮政编码

时间:2017-11-29 06:00:41

标签: r geocoding

我对地址进行了地理编码,我得到了一张如下表所示的表格。我怎样才能只提取postal_code?

library(googleway)
a <- google_geocode(address = '1208 3RD AVE S,NASHVILLE,TN', key = "ABCDEFGHIJKLMN")

a$results$address_components
[[1]]
     long_name      short_name                                  types
1             1208            1208                          street_number
2 3rd Avenue South       3rd Ave S                                  route
3  South Nashville South Nashville                neighborhood, political
4        Nashville       Nashville                    locality, political
5  Davidson County Davidson County administrative_area_level_2, political
6        Tennessee              TN administrative_area_level_1, political
7    United States              US                     country, political
8            37210           37210                            postal_code
9             4104            4104                     postal_code_suffix

1 个答案:

答案 0 :(得分:1)

使用base功能:

subset(a$results$address_components[[1]], types == "postal_code")

使用tidyverse

a$results$address_components[[1]] %>% filter(types == "postal_code")

但我想您想要提取所有列表,因此您可以使用lapply获取邮政编码列表:

lapply(a$results, function(x){
  subset(x[[1]], types == "postal_code")[["short_name"]]
})

或再次tidyverse获取数据框:

map(a$results, filter, .data = .[[1]], types == "postal_code") %>% 
  reduce(bind_rows) %>% 
  select(short_name) # optional...