将嵌套的JSON解析为R中的数据框 [英] Parse nested JSON to Data Frame in 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 ....
如果您想要完整的数据,可以在以下位置获取: http://pastebin.com/HQDf2ase
If you want the full data you can grab it at: http://pastebin.com/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中的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!