ggmap
주소 목록에서 국가 이름을 얻기 위해 라이브러리를 사용 합니다.
그러나 의도 한대로 작동하지 않습니다 (적어도 여기에 설명 된대로 ).
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 = 'more'
논쟁에서 추출하기가 훨씬 쉽다고 생각합니다 . 마지막 항목은 (아마도) 항상 국가입니다.
r<-ggmap::geocode(c('harvard university', 'the vatican'), output = 'more') %>%
mutate(
country = stringr::str_extract(address, "(?<=, )[^,]*$") # everything after last comma
)
# # A tibble: 2 x 10
# lon lat type loctype address north south east west country
# <dbl> <dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
# 1 -71.1 42.4 establishment geometric_center cambridge, ma, usa 42.4 42.4 -71.1 -71.1 usa
# 2 12.5 41.9 country approximate 00120, vatican city 41.9 41.9 12.5 12.4 vatican city
또는를 사용하여 깊게 중첩 된 목록에서 '국가'를 추출 purrr::pluck
할 수 있습니다.이 변경 사항처럼 보이므로 'address_components'내에서 적절한 색인을 찾기 위해 몇 가지 작업을 수행해야 할 수도 있습니다.
library(ggmap)
library(purrr)
r<-ggmap::geocode(c('harvard university', 'the vatican'), output = 'all')
# base r
r[[1]][[1]][[1]]$address_components[[3]]$long_name # USA
r[[2]][[1]][[1]]$address_components[[1]]$long_name # Vatican City
# purrr
purrr::pluck(r, 1, 1, 1, "address_components", 3, "long_name")
purrr::pluck(r, 2, 1, 1, "address_components", 1, "long_name")
# Result:
# usa
# vatican city
편집 : 국가 이름을 추출하는 더 강력한 방법이 있습니다.
sapply(r, function(x) {
# Simplify
x_reduc <- x$results[[1]]$address_component
# Loop for 'country' component
for (i in seq_len(length(x_reduc[[1]]))) {
component_type <- x_reduc[[i]]$types[[1]]
if (component_type == 'country') return(x_reduc[[i]]$long_name)
}
})
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다