我目前正在尝试将多个(380)JSON文件合并到一个数据帧中。
我提出了以下代码,我认为应该可行。但遗憾的是我仍然遇到错误。
library(jsonlite)
multmerge <- function(mypath)
{filenames=list.files(path=mypath, full.names=TRUE)
datalist = lapply(filenames, function(x){fromJSON(file=x)})
Reduce(function(x,y) {merge(x,y)}, datalist)}
mymergeddata <- multmerge("~/Scriptie/LaLiga")
Error in fromJSON(file = x) : argument "txt" is missing, with no default
是否有人知道它有什么问题?
"@attributes": {
"id": 771041269,
"event_id": 494,
"type_id": "1",
"period_id": "2",
"min": 57,
"sec": 4,
"player_id": "17861",
"team_id": "186",
"outcome": "1",
"x": 27.5,
"y": 71.8,
"assist": "0",
"keypass": "0",
"timestamp": "2015-09-12T16:09:58.000",
"TimeStamp": {
"locale": "2015-09-12T16:14:58.000Z",
"utc": "2015-09-12T14:14:58.000Z"
},
"version": {
"lo": 1325465600,
"hi": 1605574594
}
},
"Q": [
{
"@attributes": {
"id": 1051565894,
"qualifier_id": "141",
"value": "45.1"
}
},
{
"@attributes": {
"id": 1903370784,
"qualifier_id": "140",
"value": "12.8"
}
},
{
"@attributes": {
"id": 1828708519,
"qualifier_id": "212",
"value": "23.8"
}
},
{
"@attributes": {
"id": 1347694276,
"qualifier_id": "5",
"value": ""
}
},
{
"@attributes": {
"id": 1265264016,
"qualifier_id": "56",
"value": "Back"
}
},
{
"@attributes": {
"id": 1627212783,
"qualifier_id": "152",
"value": ""
}
},
{
"@attributes": {
"id": 1369689442,
"qualifier_id": "213",
"value": "4.0"
}
}
]
},
答案 0 :(得分:0)
让nl.json
成为您的输入文件。我调整如下json(删除“@”并在文件cf.附件的开头和结尾包括括号)
{"attributes": {
"id": 771041269,
"event_id": 494,
"type_id": "1",
"period_id": "2",
"min": 57,
"sec": 4,
"player_id": "17861",
"team_id": "186",
"outcome": "1",
"x": 27.5,
"y": 71.8,
"assist": "0",
"keypass": "0",
"timestamp": "2015-09-12T16:09:58.000",
"TimeStamp": {
"locale": "2015-09-12T16:14:58.000Z",
"utc": "2015-09-12T14:14:58.000Z"
},
"version": {
"lo": 1325465600,
"hi": 1605574594
}
},
"Q": [
{
"@attributes": {
"id": 1051565894,
"qualifier_id": "141",
"value": "45.1"
}
},
{
"@attributes": {
"id": 1903370784,
"qualifier_id": "140",
"value": "12.8"
}
},
{
"@attributes": {
"id": 1828708519,
"qualifier_id": "212",
"value": "23.8"
}
},
{
"@attributes": {
"id": 1347694276,
"qualifier_id": "5",
"value": ""
}
},
{
"@attributes": {
"id": 1265264016,
"qualifier_id": "56",
"value": "Back"
}
},
{
"@attributes": {
"id": 1627212783,
"qualifier_id": "152",
"value": ""
}
},
{
"@attributes": {
"id": 1369689442,
"qualifier_id": "213",
"value": "4.0"
}
}
]
}
}
我也更改了rjson::fromJSON(file="nl.json",method = "R")
屈服于
$attributes
$attributes$id
[1] 771041269
$attributes$event_id
[1] 494
$attributes$type_id
[1] "1"
$attributes$period_id
[1] "2"
$attributes$min
[1] 57
$attributes$sec
[1] 4
$attributes$player_id
[1] "17861"
$attributes$team_id
[1] "186"
$attributes$outcome
[1] "1"
$attributes$x
[1] 27.5
$attributes$y
[1] 71.8
$attributes$assist
[1] "0"
$attributes$keypass
[1] "0"
$attributes$timestamp
[1] "2015-09-12T16:09:58.000"
$attributes$TimeStamp
$attributes$TimeStamp$locale
[1] "2015-09-12T16:14:58.000Z"
$attributes$TimeStamp$utc
[1] "2015-09-12T14:14:58.000Z"
$attributes$version
$attributes$version$lo
[1] 1325465600
$attributes$version$hi
[1] 1605574594
$Q
$Q[[1]]
$Q[[1]]$`@attributes`
$Q[[1]]$`@attributes`$id
[1] 1051565894
$Q[[1]]$`@attributes`$qualifier_id
[1] "141"
$Q[[1]]$`@attributes`$value
[1] "45.1"
$Q[[2]]
$Q[[2]]$`@attributes`
$Q[[2]]$`@attributes`$id
[1] 1903370784
$Q[[2]]$`@attributes`$qualifier_id
[1] "140"
$Q[[2]]$`@attributes`$value
[1] "12.8"
$Q[[3]]
$Q[[3]]$`@attributes`
$Q[[3]]$`@attributes`$id
[1] 1828708519
$Q[[3]]$`@attributes`$qualifier_id
[1] "212"
$Q[[3]]$`@attributes`$value
[1] "23.8"
$Q[[4]]
$Q[[4]]$`@attributes`
$Q[[4]]$`@attributes`$id
[1] 1347694276
$Q[[4]]$`@attributes`$qualifier_id
[1] "5"
$Q[[4]]$`@attributes`$value
[1] ""
$Q[[5]]
$Q[[5]]$`@attributes`
$Q[[5]]$`@attributes`$id
[1] 1265264016
$Q[[5]]$`@attributes`$qualifier_id
[1] "56"
$Q[[5]]$`@attributes`$value
[1] "Back"
$Q[[6]]
$Q[[6]]$`@attributes`
$Q[[6]]$`@attributes`$id
[1] 1627212783
$Q[[6]]$`@attributes`$qualifier_id
[1] "152"
$Q[[6]]$`@attributes`$value
[1] ""
$Q[[7]]
$Q[[7]]$`@attributes`
$Q[[7]]$`@attributes`$id
[1] 1369689442
$Q[[7]]$`@attributes`$qualifier_id
[1] "213"
$Q[[7]]$`@attributes`$value
[1] "4.0"
答案 1 :(得分:0)
给定一个将特定JSON数据读入数据帧的函数decode_json
,您可以有效地将多个文件的解码组合成一个最终数据帧,如下所示:
import_json_files <- function(files) {
all <- lapply(files, fromJSON)
dec <- lapply(all, decode_json)
bind_rows(dec)
}