I have a data set in a nested list, so that each member of the top list has descriptors (but not necessarily the same descriptors), and then a list of children, and these children can have children, etc., The depth of the children is arbitrary. A small minimal example:
family <- list(
list(name = "Alice", age = 40, eyes = "blue", children = list(
list(name = "Bob", age = 20, eyes = "blue"),
list(name = "Charlie", age = 18, eyes = "brown")
)),
list(name = "Dan", age = 12, eyes = "green"),
list(name = "Erin", age = 69, eyes = "green", children = list(
list(name = "Frank", age = 45, eyes = "blue", children = list(
list(name = "George", age = 24, eyes = "blue", children = list(
list(name = "Harry", age = 2, eyes = "green")
)),
list(name = "Ingrid", age = 22, eyes = "brown", hair = "brown"),
list(name = "Jack", age = 29, eyes = "brown")
)),
list(name = "Karen", age = 43),
list(name = "Larry", age = 21, eyes = "blue")
))
)
> str(family, max.level = 2)
List of 3
$ :List of 4
..$ name : chr "Alice"
..$ age : num 40
..$ eyes : chr "blue"
..$ children:List of 2
$ :List of 3
..$ name: chr "Dan"
..$ age : num 12
..$ eyes: chr "green"
$ :List of 4
..$ name : chr "Erin"
..$ age : num 69
..$ eyes : chr "green"
..$ children:List of 3
Ideally, I would like to create data tactics so that each row is a member of the family, and each column is an attribute from the list:
name age eyes
1 Alice 40 blue
2 Bob 20 blue
3 Charlie 18 brown
(etc)
But it is unclear how to do this recursively to arbitrary depth. I managed to get top level members using map(family, ~ .$name)
, but I don't understand how to go deeper. This is complicated by the fact that some members do not have children.
purrr, , . , .
. !