2016-04-30 76 views
1

我有这样一个分层ID的字符向量:R:创建字符向量从ID的嵌套列表

ids <- c("0","1","2","3","1.1","1.2","1.3","2.1","2.2","2.11","2.21","2.22") 

分层结构如下:

1 
    1.1 
    1.2 
    1.3 
2 
    2.1 
     2.11 
    2.2 
     2.21 
     2.22 

我想使用从networkD3包中的diagonalNetwork()可视化此层次结构。但diagonalNetwork()需要确定每个节点的孩子像这样的嵌套列表:

l <- list(name = "0", 
      children = list(
       list(name = "1", 
        children = list(
        list(name = "1.1"), 
        list(name = "1.2"), 
        list(name = "1.3") 
        ) 
      ), 
       list(name = "2", 
        children = list(
        list(name = "2.1", 
          children = list(
          list(name = "2.11") 
         ) 
        ), 
        list(name = "2.2", 
          children = list(
          list(name = "2.21"), 
          list(name = "2.22") 
         ) 
        ) 
        ) 
      ) 
      ) 
) 

我的实际集ID是更大和更深的(最多6个位数),所以我需要一种方法来自动创建该嵌套列表。我开始像这样的几列创建存储的ID数字的data.frame:

df <- data.frame(root = 0, 
       a = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2), 
       b = c(NA, 1, 2, 3,NA, 1, 1, 2, 2, 2), 
       c = c(NA,NA,NA,NA,NA,NA, 1,NA, 1, 2)) 

但我不能想办法得到任何进一步与我所关心的。 有没有更有希望的方法?

回答

2

这是一个基于递归函数的可能解决方案。这绝不是一个快速的解决方案,但应该为你工作。

library(network3D) 
findChildren <- function(pa, ids) { 
    lapply(ids, function(ch){ 
     if(grepl(paste("^", pa, sep = ""), ch) && ch != pa && 
      nchar(gsub("\\.", "", ch)) == nchar(gsub("\\.", "", pa)) + 1) 
     { 
      childrenTmp = Filter(Negate(is.null), findChildren(ch, ids)) 
      if(length(childrenTmp) != 0) list(name = ch, children = childrenTmp) 
      else list(name = ch) 
     } 
    } 
    ) 
} 

myList <- list(name = "0", 
       children = Filter(
        function(x){nchar(x$name) == 1 },      
        lapply(ids[-1], function(id) { 
         childrenTmp = Filter(Negate(is.null), findChildren(id, ids)) 
         if(length(childrenTmp) != 0) list(name = id, children = childrenTmp) 
         else list(name = id) 
        } 
        ) 
       ) 
       ) 
diagonalNetwork(myList) 

enter image description here