Using the rmatio package , I get nested lists similar to the following:
nestedlist <- list(
a = list( a = list(1:10), b = list(35)),
b = list(11:25)
)
Ideally, I want it to look like this (all lists with one unnamed element replaced by an element):
nestedlist <- list(a = list(a=1:10, b=35), b = 11:25)
I tried already tried the following:
unlist(nestedlist)
selective_unlist <- function(e)
if(is.list(e) &&is.null(names(e))) unlist(e) else e
rapply(nestedlist, how='replace', selective_unlist)
lapply(nestedlist, selective_unlist)
recursive_selective_unlist <- function(e)
if(is.list(e)) {
if(is.null(names(e))) unlist(e)
else lapply(e, recursive_selective_unlist)
} else e
Is there a better / faster way to simplify these nested lists or is it the most recursive function?
source
share