【问题标题】:How do I deal with a nested list() that occurs when reading JSON files in R?如何处理在 R 中读取 JSON 文件时发生的嵌套 list()?
【发布时间】:2019-07-29 11:35:38
【问题描述】:

我有几个 JSON 文件应该使用 r 读取和合并。每个文件包含 51 次观察的数据。但是,当我在 r 中读取 JSON 文件时,我需要的信息嵌套在“提及”列中。我需要“提及”中包含的时间戳来创建一个新变量,该变量计算 2017 年 t 月的提及次数。

结果应该是一个数据框,其中包含 ID、t、t2 ... t12 中的 Amentions 和 t1、t2 ... t12 中的 Bmentions。因此,每个 JSON 文件包含 51 行和 25 列的数据框。

我使用了jsonlite包,写了如下代码:

jsondata1 <- stream_in(file("1595450.txt"))
%>% jsonlite::flatten()
%>% as_data_frame()    
head(jsondata1)
ID                  mentions
12345               list(Amentions = list(license = "xxx", author =    
                    list(name = "Max M", url = 
                    "http://mentionexample.com/MaxM/", m_id = "123456", 
                    posted_on = "2017-03-20T21:35:57+00:00"))
12346               list()
12347              list(Bmentions = list(license = "xxx", title =   
                    "A new star is born", url = "http://...", author =    
                    list(url = "http://www...", c_ids = list(123455), 
                    posted_on = "2017-05-17T23:57:41+00:00"), Amentions 
                    =  list(license = "xxx", author = list(name = "Max 
                    M", url = "http://mentionexample.com/MaxM/", m_id = 
                    "123456", posted_on = "2017-03-20T21:35:57+00:00")
123489             list()

目前无法正确读取 JSON 文件,但“提及”列中的数据是嵌套的。因此,第一列 ID 是正确的,但第二列不正确。

【问题讨论】:

    标签: r json jsonlite


    【解决方案1】:

    在将数据转换为 data.frame 之前,尝试在第一个管道的末尾执行 %&gt;% unlist()。它可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 2015-09-13
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多