I have the following data.frame
d
:
x <- data.frame(a=letters[1:3], b=letters[4:6], c=letters[7:9], stringsAsFactors=F)
d <- tidyr::expand(x)
d
Source: local data frame [27 x 3]
a b c
1 a d g
2 a d h
3 a d i
4 a e g
5 a e h
6 a e i
7 a f g
8 a f h
9 a f i
10 b d g
11 b d h
12 b d i
13 b e g
14 b e h
15 b e i
16 b f g
17 b f h
18 b f i
19 c d g
20 c d h
21 c d i
22 c e g
23 c e h
24 c e i
25 c f g
26 c f h
27 c f i
I'd like to get a nested list like this (to work with JSON and stuff like that):
$a
$a$d
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$a$e
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$a$f
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$b
$b$d
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$b$e
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$b$f
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$c
$c$d
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$c$e
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
$c$f
[1] "g" "h" "i" "g" "h" "i" "g" "h" "i"
To do this, I used the function tree
below
tree <- function(d) {
aux <- with(d, split(b, list(a)))
res <- lapply(aux, function(x) with(d, split(c, list(x))))
res
}
Now, let's say I have a data.frame
hierarchical with n
columns. How do I create a nested list?
Thank you!
Related Questions