将嵌套的JSON解析为R中的数据框

编程入门 行业动态 更新时间:2024-10-11 21:21:26
本文介绍了将嵌套的JSON解析为R中的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用非常讨厌的嵌套JSON时遇到了麻烦.

i'm having trouble with a very nasty nested JSON.

格式如下

{ "matches": [ { "matchId": 1, "region": "BR", "participants": [ { "participantId": 0, "teamId": 200, "stats": { "winner": true, "champLevel": 16, "item0": 3128, } { "matchId": 2, "region": "BR", "participants": [ { "participantId": 0, "teamId": 201, "stats": { "winner": false, "champLevel": 18, "item0": 3128, "item1": 3157, "item1": 3158, }

您可以在第二个匹配项中看到增加的项目数,但是在数据框中,第一行将具有相同的列:

As you can see in the second match the number of items increased, but in the data frame the first row will have the same collumns:

MatchId region ... stats.winner stats.champLevel stats.item0 stats.item1 stats.item2 1 BR TRUE 16 3128 1 BR 1 BR TRUE 16 3128 3157 3158

看到第一行小于第二行,因此R回收值....

See the first row is smaller than the second, so R recycle the values ....

如果您想要完整的数据,可以在以下位置获取: pastebin/HQDf2ase

If you want the full data you can grab it at: pastebin/HQDf2ase

我如何将json解析为data.frame:

How I parsed the json to data.frame:

json.matchData <- fromJSON(file="file.json"))

取消列出Json的元素并将其转换为数据框

matchData.i <- lapply(json.matchData$matches, function(x){ unlist(x)})

转换为数据框

matchData <- do.call("rbind", matchData.i) matchData <- as.data.frame(matchData)

但是数据帧被弄乱了,因为某些字段应该是NA,但是它们填充了错误的值.

But the dataframe is messed up, because some fields should be NA but they are filled with wrong values.

推荐答案

我认为在这里使用plyr rbind.fill()函数会有所帮助.怎么样

I think using the plyr rbind.fill() function would be helpful here. How about this

library(plyr) matchData <- rbind.fill(lapply(matchData.i, function(x) do.call("data.frame", as.list(x)) ))

lapply()位用于将中间列表转换为rbind.fill所需的data.frames.

the lapply() bit is to turn the intermediate lists into data.frames which rbind.fill requires.

更多推荐

将嵌套的JSON解析为R中的数据框

本文发布于:2023-11-30 19:28:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数据   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!