gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:47:32 26 4
gpt4 key购买 nike

我一直在使用 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

    在部分匹配的情况下,结果可以显示为数据框中的两行,或者显示为一组额外的列,我不太在意..

    任何帮助是极大的赞赏!

    干杯,

    吕克

    最佳答案

    你可以试试这样的:

    df %>% 
    unnest(col = GoogleResult) %>% unnest(col = GoogleResult)%>%
    filter(lengths(GoogleResult)>0)%>%
    {map2(.$GoogleResult,.$address,
    ~cbind(address = .y,data.frame(fromJSON(toJSON(.x))))%>%unnest())}%>%
    plyr::rbind.fill()

    关于R 在 R 中展平不同长度的嵌套列表(Google 地理编码 API 输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62033404/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com