gpt4 book ai didi

r - Google Places API 和 R - 调用数据框中的第二列返回六个单独的列

转载 作者:行者123 更新时间:2023-12-02 09:12:19 25 4
gpt4 key购买 nike

我正在尝试存储通过 Google Places API 从列表中检索到的数据框的结果。我对 API 的调用...

library(googleway)

HAVE_PLACES <- google_places(search_string = "grocery store",
location = c(35.4168, -80.5883),
radius = 10000, key = key)

...返回列表对象HAVE_PLACES:

enter image description here

此列表中的第三个对象 - 结果 - 是一个数据帧,其中包含对 API 调用中检索到的每个位置的一个观察结果。当我调用 View(HAVE_PLACES$results) 时,我得到了看起来像一组向量的内容 - 正如我在查看数据框时所期望的那样......

enter image description here

...但看起来数据框包含数据框:

enter image description here

这是怎么回事?

enter image description here

更具体地说:

  1. 数据框如何包含数据框,为什么 View() 像矢量一样显示嵌套数据框?
  2. 在处理此类数据时,您希望View() 中看到的列只是向量 - 用于操作和导出目的 - 是有什么最佳实践吗?我即将将这个名为 geometry 的所谓数据帧的每个向量转换为单独的对象,并将 cbind() 结果转换为 HAVE_PLACES$results >。但这感觉很疯狂。

最佳答案

阿克伦是对的(像往常一样!)。 data.frame 可以将列表作为“列”。这是正常行为。

您的问题似乎是一个关于如何在 R 中提取嵌套列表数据的更普遍的问题,但使用 Google 的 API 响应作为示例。鉴于您正在使用 googleway(我是该软件包的作者),我将在 Google 响应的上下文中回答它。然而,网上还有许多关于如何在 R 中使用列表的其他答案和示例。

说明

您会在结果中看到嵌套列表,因为从 Google API 返回的数据实际上是 JSON。 google_places() 函数在内部使用 jsonlite::fromJSON() 将其“简化”为 data.frame

如果您在函数调用中设置simplify = F,您可以看到原始 JSON 输出

library(googleway)

set_key("GOOGLE_API_KEY")

HAVE_PLACES_JSON <- google_places(search_string = "grocery store",
location = c(35.4168, -80.5883),
radius = 10000,
simplify = F)

## run this to view the JSON.
jsonlite::prettify(paste0(HAVE_PLACES_JSON))

您将看到 JSON 可以包含许多嵌套对象。当转换为 R data.frame 时,这些嵌套对象将作为列表列返回

如果您不熟悉 JSON,可能值得进行一些研究以了解它的全部内容。


提取数据

我编写了一些函数来从 API 响应中提取有用的信息,这可能会有所帮助

locations <- place_location(HAVE_PLACES)
head(locations)
# lat lng
# 1 35.38690 -80.55993
# 2 35.42111 -80.57277
# 3 35.37006 -80.66360
# 4 35.39793 -80.60813
# 5 35.44328 -80.62367
# 6 35.37034 -80.54748

placenames <- place_name(HAVE_PLACES)
head(placenames)
# "Food Lion" "Food Lion" "Food Lion" "Food Lion" "Food Lion" "Food Lion"

但是,请注意,您仍然会返回一些列表对象,因为在这种情况下,“位置”可以有许多“类型”

placetypes <- place_type(HAVE_PLACES)
str(placetypes)

# List of 20
# $ : chr [1:5] "grocery_or_supermarket" "store" "food" "point_of_interest" ...
# $ : chr [1:5] "grocery_or_supermarket" "store" "food" "point_of_interest" ...
# $ : chr [1:5] "grocery_or_supermarket" "store" "food" "point_of_interest" ...
# $ : chr [1:5] "grocery_or_supermarket" "store" "food" "point_of_interest" ...

摘要

通过 Google 的 API 响应,您必须提取所需的特定数据元素并将它们构建到所需的对象中

df <- cbind(
place_name(HAVE_PLACES)
, place_location(HAVE_PLACES)
, place_type(HAVE_PLACES)[[1]] ## only selecting the 1st 'type'
)

head(df)

# place_name(HAVE_PLACES) lat lng place_type(HAVE_PLACES)[[1]]
# 1 Food Lion 35.38690 -80.55993 grocery_or_supermarket
# 2 Food Lion 35.42111 -80.57277 store
# 3 Food Lion 35.37006 -80.66360 food
# 4 Food Lion 35.39793 -80.60813 point_of_interest
# 5 Food Lion 35.44328 -80.62367 establishment
# 6 Food Lion 35.37034 -80.54748 grocery_or_supermarket

关于r - Google Places API 和 R - 调用数据框中的第二列返回六个单独的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844764/

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