2017-04-22 1 views
0

私は100,000個以上のjsonファイルのリストを持っており、そこからいくつかの変数だけを持つdata.tableを取得したいと考えています。残念ながら、ファイルは複雑です。各JSONファイルの内容は、次のようになります。jsonファイルのリストからdata.tableへ:部分変数リスト

サンプル1

$id 
[1] "10.1" 
$title 
$title$value 
[1] "Why this item" 
$itemsource 
$itemsource$id 
[1] "AA" 
$date 
[1] "1992-01-01" 
$itemType 
[1] "art" 
$creators 
list() 

サンプル2

$id 
[1] "10.2" 
$title 
$title$value 
[1] "We need this item" 
$itemsource 
$itemsource$id 
[1] "AY" 
$date 
[1] "1999-01-01" 
$itemType 
[1] "art" 
$creators 
    type    name firstname surname affiliationIds 
1 Person Frank W. Cornell. Frank W. Cornell.    a1 
2 Person David A. Chen. David A. Chen.    a1 

$affiliations 
    id           name 
1 a1 Foreign Affairs Desk, New York Times 

私がこのファイルのセットから必要なことは、作成者名、アイテムIDと日付を持つテーブルです。上記の2つのサンプルファイルの場合:

id   date   name    firstname lastname creatortype 
"10.1"  "1992-01-01"  NA     NA  NA  NA 
"10.2"  "1999-01-01" Frank W. Cornell.  Frank W. Cornell. Person 
"10.2"  "1999-01-01" David A. Chen.   David A. Chen.  Person 

私がこれまでに行っているもの:

library(parallel) 
library(data.table) 
library(jsonlite) 
library(dplyr) 

filelist = list.files(pattern="*.json",recursive=TRUE,include.dirs =TRUE) 
parsed = mclapply(filelist, function(x) fromJSON(x),mc.cores=24) 
data = rbindlist(mclapply(1:length(parsed), function(x) { 
    a = data.table(item = parsed[[x]]$id, date = list(list(parsed[[x]]$date)), name = list(list(parsed[[x]]$name)), creatortype = list(list(parsed[[x]]$creatortype))) #ignoring the firstname/lastname fields here for convenience 
    b = data.table(id = a$item, date = unlist(a$date), name=unlist(a$name), creatortype=unlist(a$creatortype)) 
    return(b) 
},mc.cores=24)) 

しかし、最後のステップで、私はこのエラーを取得:事前に

"Error in rbindlist(mclapply(1:length(parsed), function(x){: 
Item 1 of list is not a data.frame, data.table or list" 

感謝をあなたの提案のために。 関連した質問が含まれます: エラーメッセージからExtract data from list of lists [R] R convert json to list to data.table I want to convert JSON file into data.table in r How can read files from directory using R? Convert R data table column from JSON to data table

答えて

1

、私はこれは基本的にmclapplyからの結果の一つは、()空で、私はNULLまたはデータのいずれかを意味し、空であることを意味しているとします。テーブルを0行にするか、または単に並列処理内でエラーに遭遇します。あなたは何ができるか

は次のとおりです。

  1. は(mclapply内部よりチェックを追加)のようなbが

  2. 空であるかどうか、エラーを試すか、BとBのnrowのクラスをチェックします

    rbindlistを使用すると、引数fill = Tを追加すると、

希望が解決します。

関連する問題