我有一个像这样的层次结构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
我想diagonalNetwork()
从networkD3
包中使用以可视化此层次结构。但是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位数字),因此我需要一种自动创建此嵌套列表的方法。我首先创建一个data.frame,将id的数字存储在像这样的几列中:
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))
但我想不出一种进一步解决我的问题的方法。有没有更有前途的方法?
这是基于递归函数的可能解决方案。这绝不是一个快速的解决方案,而是应该为您服务。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句