Is there an option that allows me to retain list entries that have values that are 0-length lists in the how="melt" option? In the below, I’d like to retain the B entry:
library(rrapply)
lst <- list(A=1, B=list(), C=list(D=letters[1:3]))
str(lst)
#> List of 3
#> $ A: num 1
#> $ B: list()
#> $ C:List of 1
#> ..$ D: chr [1:3] "a" "b" "c"
# Is there an option where B is not dropped due to being an empty list?
rrapply(lst, how="melt")
#> L1 L2 value
#> 1 A <NA> 1
#> 2 C D a, b, c
Created on 2023-04-27 with reprex v2.0.2
>Solution :
The empty list elements can be recursively changed to NA and then melt
rrapply(lst, classes = "list", f = \(x) if(!length(x)) NA else x) |>
rrapply(how = "melt")
-output
L1 L2 value
1 A <NA> 1
2 B <NA> NA
3 C D a, b, c