【问题标题】:I am accessing an api for data and am not able to the columns in r我正在访问数据的 api,但无法访问 r 中的列
【发布时间】:2016-09-11 20:04:00
【问题描述】:

我正在使用 httr 从 api 访问数据,然后保存该数据,以便我可以开始进行一些数据验证。我在访问我认为的列和行时遇到问题,因为数据已存储为嵌套列表。

query2 <- "this is where my api key in addition to the website I was trying went "
out2 <- GET(url = query2)
http_status(out2)
$category
[1] "Success"

$reason
[1] "OK"

$message
[1] "Success: (200) OK"

dataContent <- content(out2)
names(dataContent)
[1] "Data"

envData <- jsonlite::fromJSON(toJSON(dataContent))
result <- data.frame(envData)
names(result)
[1] "Data.Providers.Name"    "Data.Providers.Type"   
[3] "Data.Providers.Owner"   "Data.Providers.Records"
# when I view the data I can see it in rows and columns
View(result$Data.Providers.Records)

#for some reason its saved as one object and I cannot access individual         rows and columns 
length(cleanerData)
[1] 1

【问题讨论】:

  • 你尝试过索引吗?

标签: r api httr jsonlite


【解决方案1】:

我目前无法复制您的示例,但您可以通过使用 fromJSON 的 flatten = TRUE 获得您期望的 data.frame

query2 <- "api-url"
out2 <- GET(url = query2)

## assuming its JSON content, parse as text and use fromJSON
dataContent <- content(out2, as = "text")
parsed_content <- jsonlite::fromJSON(dataContent, flatten = TRUE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 2018-01-11
    • 2023-01-09
    • 1970-01-01
    • 2018-04-18
    • 2020-06-12
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多