R中不同长度的拼合嵌套列表(Google地理编码API输出)R

问题描述 投票:2回答:1

我一直在使用Google的地址解析API对地址列表进行地址解析。它以嵌套列表的形式返回结果。列表中的元素可能会有所不同,有时会有部分匹配,从而导致在最高级别嵌套多个嵌套列表。到目前为止,我已经将每个GoogleResult保存到一个数据框单元格中。

这是我的数据框示例:

    df <- structure(list(address = structure(c(3L, 1L, 2L), .Label = c("115 Civic Parade, Altona VIC 3018", 
"Civic Parade, Altona VIC 3018", "EAST LA CLARKEFIELD 3430"), class = "factor"), 
    GoogleResult = list(list(list(access_points = list(), address_components = list(
        list(long_name = "Los Angeles", short_name = "Los Angeles", 
            types = list("locality", "political")), list(long_name = "Los Angeles County", 
            short_name = "Los Angeles County", types = list("administrative_area_level_2", 
                "political")), list(long_name = "California", 
            short_name = "CA", types = list("administrative_area_level_1", 
                "political")), list(long_name = "United States", 
            short_name = "US", types = list("country", "political"))), 
        formatted_address = "Los Angeles, CA, USA", geometry = list(
            bounds = list(northeast = list(lat = 34.3373061, 
                lng = -118.1552891), southwest = list(lat = 33.7036519, 
                lng = -118.6681759)), location = list(lat = 34.0522342, 
                lng = -118.2436849), location_type = "APPROXIMATE", 
            viewport = list(northeast = list(lat = 34.3373061, 
                lng = -118.1552891), southwest = list(lat = 33.7036519, 
                lng = -118.6681759))), partial_match = TRUE, 
        place_id = "ChIJE9on3F3HwoAR9AhGJW_fL-I", types = list(
            "locality", "political")), list(access_points = list(), 
        address_components = list(list(long_name = "3430", short_name = "3430", 
            types = list("postal_code")), list(long_name = "Clarkefield", 
            short_name = "Clarkefield", types = list("locality", 
                "political")), list(long_name = "Victoria", short_name = "VIC", 
            types = list("administrative_area_level_1", "political")), 
            list(long_name = "Australia", short_name = "AU", 
                types = list("country", "political"))), formatted_address = "Clarkefield VIC 3430, Australia", 
        geometry = list(bounds = list(northeast = list(lat = -37.4364578, 
            lng = 144.8986988), southwest = list(lat = -37.5280439, 
            lng = 144.7012193)), location = list(lat = -37.497542, 
            lng = 144.8071366), location_type = "APPROXIMATE", 
            viewport = list(northeast = list(lat = -37.4364578, 
                lng = 144.8986988), southwest = list(lat = -37.5280439, 
                lng = 144.7012193))), partial_match = TRUE, place_id = "ChIJS3IdP-xX1moRkD8uRnhWBBw", 
        types = list("postal_code"))), list(list(access_points = list(), 
        address_components = list(list(long_name = "115", short_name = "115", 
            types = list("street_number")), list(long_name = "Civic Parade", 
            short_name = "Civic Parade", types = list("route")), 
            list(long_name = "Altona", short_name = "Altona", 
                types = list("locality", "political")), list(
                long_name = "Hobsons Bay City", short_name = "Hobsons Bay", 
                types = list("administrative_area_level_2", "political")), 
            list(long_name = "Victoria", short_name = "VIC", 
                types = list("administrative_area_level_1", "political")), 
            list(long_name = "Australia", short_name = "AU", 
                types = list("country", "political")), list(long_name = "3018", 
                short_name = "3018", types = list("postal_code"))), 
        formatted_address = "115 Civic Parade, Altona VIC 3018, Australia", 
        geometry = list(bounds = list(northeast = list(lat = -37.8633208, 
            lng = 144.8316509), southwest = list(lat = -37.86409, 
            lng = 144.8303929)), location = list(lat = -37.863727, 
            lng = 144.8310159), location_type = "ROOFTOP", viewport = list(
            northeast = list(lat = -37.8623564197085, lng = 144.832370880292), 
            southwest = list(lat = -37.8650543802915, lng = 144.829672919709))), 
        place_id = "ChIJBXz75NRj1moRpVRt21nooQw", types = list(
            "premise"))), list(list(access_points = list(), address_components = list(
        list(long_name = "Civic Parade", short_name = "Civic Parade", 
            types = list("route")), list(long_name = "Altona", 
            short_name = "Altona", types = list("locality", "political")), 
        list(long_name = "Hobsons Bay City", short_name = "Hobsons Bay", 
            types = list("administrative_area_level_2", "political")), 
        list(long_name = "Victoria", short_name = "VIC", types = list(
            "administrative_area_level_1", "political")), list(
            long_name = "Australia", short_name = "AU", types = list(
                "country", "political")), list(long_name = "3018", 
            short_name = "3018", types = list("postal_code"))), 
        formatted_address = "Civic Parade, Altona VIC 3018, Australia", 
        geometry = list(bounds = list(northeast = list(lat = -37.8626502, 
            lng = 144.8449271), southwest = list(lat = -37.8661171, 
            lng = 144.81081)), location = list(lat = -37.864412, 
            lng = 144.8303004), location_type = "GEOMETRIC_CENTER", 
            viewport = list(northeast = list(lat = -37.8626502, 
                lng = 144.8449271), southwest = list(lat = -37.8661171, 
                lng = 144.81081))), place_id = "EihDaXZpYyBQYXJhZGUsIEFsdG9uYSBWSUMgMzAxOCwgQXVzdHJhbGlhIi4qLAoUChIJtbGXUCti1moRKcxHhdx2QrYSFAoSCSEyccGdYdZqEXDajCF1VgQF", 
        types = list("route"))))), row.names = c(NA, -3L), class = "data.frame")

第一种情况具有部分匹配,其中有两个嵌套的结果列表。

我的预期输出是:

  • 将所有列表的所有元素作为列的数据框
  • 所有要适当命名的列
  • 部分匹配具有> 1个结果,可以是每个匹配1行,也可以只是使用'address2'变量加宽数据框。无论哪种方式,我都可以使用。

我尝试过类似的事情:

lapply(df$GoogleResult, data.frame, stringsAsFactors = FALSE)

但是元素的长度不同...导致:

arguments imply differing number of rows: 0, 1

如果发生部分匹配,结果可以显示为数据帧中的两行,也可以显示为其他列,我不在乎。

非常感谢您的帮助!

干杯,

Luc

我一直在使用Google的地址解析API对地址列表进行地址解析。它以嵌套列表的形式返回结果。列表中的元素可能会有所不同,有时会出现部分匹配,从而导致...

r list geocoding nested-lists google-geocoder
1个回答
1
投票

您可以尝试以下方法:

© www.soinside.com 2019 - 2024. All rights reserved.