我想使用从R输出的JSON文件创建treemap(或者Python,如果我真的可以在R中执行此操作,我可以切换到Python?)。该 数据看起来像this:
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}
]
} ...
答案 0 :(得分:0)
也许这样的事情可以胜任这项工作? Split
列cat
上的数据框,然后从每个结果数据框中删除第一列,并使用toJSON
包中的jsonlite
转换为JSON。
JSONoutput <- jsonlite::toJSON(lapply(split(data, data$cat), function(x) {x[,-1]}))