Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retain empty lists in rrapply::rrapply()

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading