階層情報を含む data.frame を JSON (またはネストされたリスト) に適切に変換するにはどうすればよいですか?
次の data.frame があるとします。
df <- data.frame(
id = c('1', '1.1', '1.1.1', '1.2'),
value = c(10, 5, 5, 5))
# id value
# 1 10
# 1.1 5
# 1.1.1 5
# 1.2 5
次に、次の JSON で終了したいと思います。
{
"id": "1",
"value": 10,
"children": [
{
"id": "1.1",
"value": 5,
"children": [
{
"id": "1.1.1",
"value": 5
}
]
},
{
"id": "1.2",
"value": 5
}
]
}
whereid
は階層構造を定義.
する区切り文字です。
私の意図は、データを R から階層的な D3 ビジュアライゼーション(たとえば、パーティション レイアウトまたはズーム可能なツリーマップ) に簡単に変換できるようにすることです。さらに「値」列を追加できるとよいでしょう。例: 、、など。value
size
weight
ありがとうございました!
編集:元の質問に戻したので、すべての回答を簡単に確認できます (すべての編集で申し訳ありません)。