Clearly a case for a recursive function, but getting the return values to unlist properly is tricky. Here's a function that will do it; it doesn't get the names quite right but that's 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)))
}
}
Output from unnest(l)
is
$all
[1] 1 2 3 4 5 6 7 8 9 10
$A.all
[1] 1 2
$A.a
[1] 1
$A.b
[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
$B.e
[1] 9 10
and can be massaged into your desired output with
out <- unnest(l)
names(out) <- sub("\\.*all", "", names(out))
out[-1]
To not recurse when there's only one element, 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)))
}
}