我有一个JSON文件.虽然原始文件非常大,但出于这个问题的目的,我将其缩减为一个小得多的可重现示例(无论大小如何,我仍然会得到相同的错误):

{
  "relationships_followers": [
    {
      "title": "",
      "media_list_data": [
        
      ],
      "string_list_data": [
        {
          "href": "https://www.instagram.com/testaccount1",
          "value": "testaccount1",
          "timestamp": 1669418204
        }
      ]
    },
    {
      "title": "",
      "media_list_data": [
        
      ],
      "string_list_data": [
        {
          "href": "https://www.instagram.com/testaccount2",
          "value": "testaccount2",
          "timestamp": 1660426426
        }
      ]
    },
    {
      "title": "",
      "media_list_data": [
        
      ],
      "string_list_data": [
        {
          "href": "https://www.instagram.com/testaccount3",
          "value": "testaccount3",
          "timestamp": 1648230499
        }
      ]
    },
       {
      "title": "",
      "media_list_data": [
        
      ],
      "string_list_data": [
        {
          "href": "https://www.instagram.com/testaccount4",
          "value": "testaccount4",
          "timestamp": 1379513403
        }
      ]
    }
  ]
}

我正在try 将其转换为R中的数据帧,其中包含hrefvaluetimestamp个变量的值:

enter image description here

但是,当我运行以下命令时,我从另一个so答案中摘取了关于将JSON转换为R的答案:

library("rjson")

result <- fromJSON(file = "test_file.json")

json_data_frame <- as.data.frame(result)

我遇到了这个关于不同行的错误.

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

如何才能将我所拥有的内容转换为所需的df格式?

推荐答案

这是因为有嵌套的数据.

df<- as.data.frame(do.call(rbind, lapply(
  lapply(result$relationships_followers, "[[", "string_list_data"), "[[", 1)))

df
#>      href                                     value          timestamp 
#>  "https://www.instagram.com/testaccount1" "testaccount1" 1669418204
#>  "https://www.instagram.com/testaccount2" "testaccount2" 1660426426
#>  "https://www.instagram.com/testaccount3" "testaccount3" 1648230499
#>  "https://www.instagram.com/testaccount4" "testaccount4" 1379513403

注意:jsonlite包在解析数据方面做得更好.默认情况下为框架.

R相关问答推荐

对lme 4对象运行summary()时出错(diag中的错误(from,names = RST):对象unpackedMatrix_diag_get找不到)

按R中的组查找相邻列的行累积和的最大值

以R中的正确顺序将日期时间字符列转换为posixct

将包含卷的底部25%的组拆分为2行

如何根据嵌套元素的名称高效而优雅地确定它属于哪个列表?

如何在ggplot图中找到第二轴的比例

如何使用ggplot对堆叠条形图进行嵌套排序?

通过在colname中查找其相应值来创建列

在R中按行按列范围查找最大值的名称

如何在PackageStatus()中列出&q;不可用的包&q;?

在另一个包中设置断点&S R函数

从多个可选列中选取一个值到一个新列中

如何将一个方阵分解成没有循环的立方体

错误包arrowR:READ_PARQUET/OPEN_DATASET&QOT;无法反序列化SARIFT:TProtocolException:超出大小限制&Quot;

如何显示准确的p值而不是<;0.001*?

对R中的列表列执行ROW Mean操作

ArrangeGrob()和类似的替代方法不接受Grob列表.在Grid.Draw,返回:glist中的错误(...):仅允许在glist";中使用Grobs;

使用函数从R中的列中删除标高

当y为负值时,无法使stat_cor正确定位到底部?

如何在网页抓取中自动更改页码?