开发者

conversion of nested list to unnested with cumulative concatenation

I would like to convert nested li开发者_JAVA百科st like this:

l <- list(A=list(a=list(1),b=list(2)),
          B=list(cd=list(c=list(3,4,5),d=list(6,7,8)),e=list(c(9,10))))

into list

o <- list(A=c(1,2),A.a=1,A.b=2,B=c(3:10),
          B.cd=c(3:8),B.cd.c=c(3:5),B.cd.d=c(6:8),B.e=c(9:10))

At each list level values from nested lists should be concatenated.


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)))
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜