Obviously, the case is for a recursive function, but getting the return values ββfor the correct list is complicated. Here is the function that will do this; he does not receive the names completely correct, but this is easily fixed afterwards.
unnest <- function(x) { if(is.null(names(x))) { list(unname(unlist(x))) } else { c(list(all=unname(unlist(x))), do.call(c, lapply(x, unnest))) } }
Exiting unnest(l) equals
$all [1] 1 2 3 4 5 6 7 8 9 10 $A.all [1] 1 2 $Aa [1] 1 $Ab [1] 2 $B.all [1] 3 4 5 6 7 8 9 10 $B.cd.all [1] 3 4 5 6 7 8 $B.cd.c [1] 3 4 5 $B.cd.d [1] 6 7 8 $Be [1] 9 10
and can be massaged into the desired result using
out <- unnest(l) names(out) <- sub("\\.*all", "", names(out)) out[-1]
To avoid overwriting when there is only one item, try
unnest2 <- function(x) { if(is.null(names(x)) | length(x)==1) { list(unname(unlist(x))) } else { c(list(all=unname(unlist(x))), do.call(c, lapply(x, unnest2))) } }
Aaron
source share