The function rrapply()
in the rrapply
-package has an option how = "unmelt"
that converts a melted data.frame to a nested list, where each row in the data.frame becomes a node path in the nested list.
To apply this function, we first need to transform the tbl
data.frame to the input format that is required by rrapply()
:
library(purrr)
library(dplyr)
library(rrapply)
## put data.frame in format for rrapply-function
tbl1 <- imap_dfc(tbl, ~bind_cols(.y, .x)) %>%
group_by(across(num_range(prefix = "...", range = 1:5))) %>%
summarize(`...6` = list(c(`...6`)))
tbl1
#> # A tibble: 2 x 6
#> # Groups: ...1, ...2, ...3, ...4 [2]
#> ...1 ...2 ...3 ...4 ...5 ...6
#> <chr> <chr> <chr> <chr> <chr> <list>
#> 1 Col1 Var1 Col2 Var1_1 Col3 <chr [2]>
#> 2 Col1 Var1 Col2 Var1_2 Col3 <chr [2]>
## unmelt to nested list
ls_tbl <- rrapply(tbl1, how = "unmelt")
str(ls_tbl)
#> List of 1
#> $ Col1:List of 1
#> ..$ Var1:List of 1
#> .. ..$ Col2:List of 2
#> .. .. ..$ Var1_1:List of 1
#> .. .. .. ..$ Col3: chr [1:2] "Var1_1_1" "Var1_1_2"
#> .. .. ..$ Var1_2:List of 1
#> .. .. .. ..$ Col3: chr [1:2] "Var1_2_1" "Var1_2_2"
Note that the purpose of the group_by()
and summarize()
operations is only to get multiple var1_%_%
under a single Col3
node. The following is considerably easier (but does not produce exactly the same result):
ls_tbl <- rrapply(imap_dfc(tbl, ~bind_cols(.y, .x)), how = "unmelt")
str(ls_tbl)
#> List of 1
#> $ Col1:List of 1
#> ..$ Var1:List of 1
#> .. ..$ Col2:List of 2
#> .. .. ..$ Var1_1:List of 2
#> .. .. .. ..$ Col3: chr "Var1_1_1"
#> .. .. .. ..$ Col3: chr "Var1_1_2"
#> .. .. ..$ Var1_2:List of 2
#> .. .. .. ..$ Col3: chr "Var1_2_1"
#> .. .. .. ..$ Col3: chr "Var1_2_2"