我使用的是googleway
包,我有一堆地址需要解析出嵌套列表中地址的各个组成部分。循环(不鼓励(和应用函数似乎都令人困惑,我不确定是否有一个整洁的解决方案。我发现map
函数(特别是它在后端列表上调用的pluck
函数(可以实现我的目标,所以我将分享我的解决方案。
问题:我需要拿出一些关于白宫的信息,比如
- 纬度
- 经度
您需要使用googleway::set_key(API_KEY)
设置Google Cloud API密钥,但这只是嵌套列表的一个示例,我希望使用此软件包的人会看到。
# Address for the White House and the Lincoln Memorial
address_vec <- c(
"1600 Pennsylvania Ave NW, Washington, DC 20006",
"2 Lincoln Memorial Cir NW, Washington, DC 20002"
)
address_vec <- pmap(list(address_vec), googleway::google_geocode)
输出
[[1]]
[[1]]$results
address_components
1 1600, Pennsylvania Avenue Northwest, Northwest Washington, Washington, District of Columbia, United States, 20500, 1600, Pennsylvania Avenue NW, Northwest Washington, Washington, DC, US, 20500, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
formatted_address geometry.bounds.northeast.lat
1 1600 Pennsylvania Avenue NW, Washington, DC 20500, USA 38.8979
geometry.bounds.northeast.lng geometry.bounds.southwest.lat geometry.bounds.southwest.lng geometry.location.lat
1 -77.03551 38.89731 -77.03796 38.89766
geometry.location.lng geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1 -77.03657 ROOFTOP 38.89895 -77.03539
geometry.viewport.southwest.lat geometry.viewport.southwest.lng place_id
1 38.89626 -77.03808 ChIJGVtI4by3t4kRr51d_Qm_x58
types
1 establishment, point_of_interest, premise
[[1]]$status
[1] "OK"
[[2]]
[[2]]$results
address_components
1 2, Lincoln Memorial Circle Northwest, Southwest Washington, Washington, District of Columbia, United States, 20037, 2, Lincoln Memorial Cir NW, Southwest Washington, Washington, DC, US, 20037, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
formatted_address geometry.location.lat geometry.location.lng
1 2 Lincoln Memorial Cir NW, Washington, DC 20037, USA 38.88927 -77.05018
geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1 ROOFTOP 38.89062 -77.04883
geometry.viewport.southwest.lat geometry.viewport.southwest.lng place_id
1 38.88792 -77.05152 ChIJgRuEham3t4kRFju4R6De__g
plus_code.compound_code plus_code.global_code types
1 VWQX+PW Washington, DC, USA 87C4VWQX+PW street_address
[[2]]$status
[1] "OK"
以下是我从Googleway Vignette获得的一些代码:
df <- google_geocode(address = "Flinders Street Station",
key = key,
simplify = TRUE)
geocode_coordinates(df)
# lat lng
# 1 -37.81827 144.9671
看起来你需要做的是:
df <- google_geocode("1600 Pennsylvania Ave")
geocode_coordinates(df)
我提出的解决方案是一个自定义函数,可以访问列表的任何部分:
geocode_accessor <- function(df, accessor, ...) {
unlist(map(df, list(accessor, ...)))
}
这有三个重要的部分需要理解:
map
函数正在为我们调用pluck
函数(它取代了[[
的使用(。你可以在这里阅读更多关于正在发生的事情,但只要知道这可以让我们按名称访问事情- "在函数的定义以及列表中,我们可以访问多个级别。同样,使用
list()
访问列表中的其他级别在pull文档中进行了解释 - 使用unlist将列表转换为向量(我在实例中想要的(
把这些放在一起,我们可以得到白宫的纬度&林肯纪念堂:
geocode_accessor(address_vec, "results", "geometry", "location", "lat")
[1] 38.89766 38.88927