【问题标题】:Fetch Gallup data JSON page and convert to csv using R获取盖洛普数据 JSON 页面并使用 R 转换为 csv
【发布时间】:2018-05-27 11:20:50
【问题描述】:

我想从以下页面获取 R 中的 json 数据并将其转换为 tibble。第一次在 R 中使用 json。

以下代码不起作用,因此可以使用一些帮助。

library(jsonlite)
library(tidyverse)

trump_json <- fromJSON(txt = "http://news.gallup.com/wwwv7interactives/json/CURRENTPRESWEEKLY/codename.aspx?")
obama_json <- fromJSON(txt = "http://news.gallup.com/wwwv7interactives/json/OBAMAEXPANDED/codename.aspx?")

as.tibble(trump_json)

编辑:将文件更改为 txt 参数。

现在它产生了:

# A tibble: 3 x 2
     `?xml` CurrentPresident
     <list>           <list>
1 <chr [1]>        <chr [1]>
2 <chr [1]>      <list [12]>
3 <chr [1]>       <list [2]>

【问题讨论】:

  • 参数名为txt 而不是file
  • 哦,当然......你知道在tibble结果之后如何继续吗?
  • 检查你得到了什么数据:df1$CurrentPresident$data 并提取或保存或...(小标题保存在df1

标签: json r csv import


【解决方案1】:

您的问题标题与您在上面的要求不符。我会尽可能更新问题标题以反映这一点。

请在 R 中找到这个将 JSON 数据转换为 tibble 的解决方案。

library(jsonlite)

json_raw <- stream_in(url("http://news.gallup.com/wwwv7interactives/json/CURRENTPRESWEEKLY/codename.aspx?"), flatten = TRUE)

#--str(json_raw)

json_flat <- flatten(json_raw)

#--json_flat

library(tibble)

json_tibble <- as_data_frame(json_flat)

json_tibble

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 2021-08-29
    • 2017-12-13
    • 2017-09-11
    • 2019-09-07
    • 2017-03-10
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    相关资源
    最近更新 更多