【发布时间】:2021-04-09 02:02:08
【问题描述】:
我使用ggmap 库从地址列表中获取国家/地区名称。
但它没有按预期工作(至少as described here)
r<-ggmap::geocode(c('harvard university', 'the vatican'), output = 'more')
glimpse(r)
结果:
Rows: 2
Columns: 9
$ lon <dbl> -71.117, 12.453
$ lat <dbl> 42.377, 41.903
$ type <chr> "establishment", "country"
$ loctype <chr> "geometric_center", "approximate"
$ address <chr> "cambridge, ma, usa", "00120, vatican city"
$ north <dbl> 42.378, 41.907
$ south <dbl> 42.376, 41.900
$ east <dbl> -71.115, 12.458
$ west <dbl> -71.118, 12.446
在这种情况下如何获取国家名称?
更新:
如果我提供output='all' 参数,它会返回一个长嵌套列表,其中address_components 中有一个国家/地区名称。检索它的最有效方法是什么?
代码:
r<-ggmap::geocode(c('harvard university', 'the vatican'), output = 'all')
glimpse(r)
结果:
> str(r )
tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
$ results:List of 1
..$ :List of 5
.. ..$ address_components:List of 3
.. .. ..$ :List of 3
.. .. .. ..$ long_name : chr "New York"
.. .. .. ..$ short_name: chr "New York"
.. .. .. ..$ types :List of 2
.. .. .. .. ..$ : chr "locality"
.. .. .. .. ..$ : chr "political"
.. .. ..$ :List of 3
.. .. .. ..$ long_name : chr "New York"
.. .. .. ..$ short_name: chr "NY"
.. .. .. ..$ types :List of 2
.. .. .. .. ..$ : chr "administrative_area_level_1"
.. .. .. .. ..$ : chr "political"
.. .. ..$ :List of 3
.. .. .. ..$ long_name : chr "United States"
.. .. .. ..$ short_name: chr "US"
.. .. .. ..$ types :List of 2
.. .. .. .. ..$ : chr "country"
.. .. .. .. ..$ : chr "political"
.. ..$ formatted_address : chr "New York, NY, USA"
.. ..$ geometry :List of 4
.. .. ..$ bounds :List of 2
.. .. .. ..$ northeast:List of 2
.. .. .. .. ..$ lat: num 40.9
.. .. .. .. ..$ lng: num -73.7
.. .. .. ..$ southwest:List of 2
.. .. .. .. ..$ lat: num 40.5
.. .. .. .. ..$ lng: num -74.3
.. .. ..$ location :List of 2
.. .. .. ..$ lat: num 40.7
.. .. .. ..$ lng: num -74
.. .. ..$ location_type: chr "APPROXIMATE"
.. .. ..$ viewport :List of 2
.. .. .. ..$ northeast:List of 2
.. .. .. .. ..$ lat: num 40.9
.. .. .. .. ..$ lng: num -73.7
.. .. .. ..$ southwest:List of 2
.. .. .. .. ..$ lat: num 40.5
.. .. .. .. ..$ lng: num -74.3
.. ..$ place_id : chr "ChIJOwg_06VPwokRYv534QaPC8g"
.. ..$ types :List of 2
.. .. ..$ : chr "locality"
.. .. ..$ : chr "political"
$ status : chr "OK"
【问题讨论】:
-
你知道
output = "all"的国家吗?