2016-04-20 7 views
0

私はRから出力したJSONファイルを使ってtreemapを作成したいと思います(または、PythonではRでこれを本当に行うことができない場合はPythonに切り替えることができます)。私はrjsonjsonliteのドキュメントを見ていないし、どちらもそれが見えるように思わRでマルチレベルJSONを生成するにはどうすればよいですか?

cat type pop 
Buddhism Other 116237936 
Christianity Anglican 36955033 
Christianity Catholic 391332035 
Christianity Orthodox 98501171 
Christianity Other 13674466 
Christianity Mahayana 160887585 
Islam Ibadhi 62273219 
Islam Shia 19436742 
Islam Sunni 49050320 
Judaism Conservative 1426350 
Judaism Orthodox 856827 
Judaism Other 7796835 
Judaism Reform 1929388 

そして、私のようなについて見てJSONを必要とする...

{"name": "Buddhism", 
    "children": 
     {"name": "Other", "size": 116237936} 
    }, 
    { 
    "name": "Christianity", 
    "children": [ 
     {"name": "Anglican", "size": 36955033}, 
     {"name": "Catholic", "size": 391332035}, 
     {"name": "Orthodox", "size": 98501171}, 
     {"name": "Other", "size": 13674466}, 
     {"name": "Mahayana", "size": 160887585} 
    ] 
    } ... 

: データは何かthisのように見えます簡単。

+0

おそらくこの記事http://stackoverflow.com/questions/31339805/converting-json-format-to-csv-to-upload-data-table-in-r-to-produce-d3-bubble -cha/31352770#31352770助けてくれます – timelyportfolio

答えて

0

多分このようなものが仕事をするだろうか? Split列のデータフレームcatを作成し、結果の各データフレームの最初の列をドロップし、を使用してパッケージを使用してJSONに変換します。

JSONoutput <- jsonlite::toJSON(lapply(split(data, data$cat), function(x) {x[,-1]})) 
+0

私は今、 '{" "仏教":[{"タイプ": "その他"、 "ポップ":116237936} {"名前": "仏教"、 "子供":[{...}] '何かが働く。 – Amanda

+0

'JSONoutput < - jsonlite :: toJSON(lapply(split、data、data $ cat)、function(x){list(" children "= x [、 - 1})))' の場合は君は。 – user5219763

関連する問題