私はこのネストされたデータセットを持っています:
grandparent,parent,child,grandchild,age
Grandma,,,,100
Grandma,John,,,72
Grandma,John,Jessica,,41
Grandma,John,Joanne,,35
Grandma,Mary,,,70
Grandma,Mary,Max,,39
Grandma,Mary,Max,Ken,12
Grandma,Mary,Max,Kate,19
Grandma,Mary,Max,Karl,8
Grandma,Mary,Millie,Peter,2
Grandma,Mary,Millie,Pat,11
Grandma,Mary,Millie,Pam,24
Grandma,Dave,,,66
Grandma,Dave,Doloris,,32
Grandma,Dave,Dana,,23
Grandma,Dave,Daniel,,13
以下のように、各ノードで値「age」を保持しますが、D3.jsの la flame.jsonのような階層的な JSON構造に変換したいと思います。この主題が完全に新しいものではないことは知っていますが、ノードが不規則になり、各ノードの値を取得するソリューションを見たことがありません..
{
"name":"Grandma",
"age": 100,
"children":[
{
"name":"John",
"age": 72,
"children":[
{
"name":"Jessica",
"age": 41
},
{
"name":"Joanne",
"age": 35
}
]
},
{
"name":"Mary",
"age": 70,
"children":[
{
"name":"Max",
"age": 39
"children":[
{
"name":"Ken",
"age":12
},
{
"name":"Kate",
"age":19
},
{
"name":"Karl",
"age":8
}
]
},
{
"name":"Millie",
"age":43,
"children":[
{
"name":"Peter",
"age":2
},
{
"name":"Pat",
"age":11
},
{
"name":"Pam",
"age":24
}
]
}
]
},
{
"name":"Dave",
"age": 66,
"children":[
{
"name":"Doloris",
"age":32
},
{
"name":"Dana",
"age":23
},
{
"name":"Daniel",
"age":13
}
]
}
]
}
data.frame:
structure(list(grandparent = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Grandma", class = "factor"),
parent = structure(c(1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L), .Label = c("", "Dave", "John",
"Mary"), class = "factor"), child = structure(c(1L, 1L, 5L,
6L, 1L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 1L, 4L, 2L, 3L), .Label = c("",
"Dana", "Daniel", "Doloris", "Jessica", "Joanne", "Max",
"Millie"), class = "factor"), grandchild = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 4L, 3L, 2L, 7L, 6L, 5L, 1L, 1L, 1L, 1L
), .Label = c("", "Karl", "Kate", "Ken", "Pam", "Pat", "Peter"
), class = "factor"), age = c(100L, 72L, 41L, 35L, 70L, 39L,
12L, 19L, 8L, 2L, 11L, 24L, 66L, 32L, 23L, 13L)), .Names = c("grandparent",
"parent", "child", "grandchild", "age"), class = "data.frame", row.names = c(NA,
-16L))