在R中将关系数据转换为分层列表

在R中将关系数据转换为分层列表,r,hierarchical-data,networkd3,R,Hierarchical Data,Networkd3,这是我的第一个问题,;所以请温柔一点 我有以下形式的一些数据: library('networkD3') Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"), Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis

这是我的第一个问题,;所以请温柔一点

我有以下形式的一些数据:

library('networkD3')
    Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
                  Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
> Relationships
  Parent         Child
1  earth         ocean
2  earth        forest
3 forest          tree
4 forest     sasquatch
5  ocean          fish
6  ocean       seaweed
7  ocean mantis shrimp
8  ocean   sea monster
像这样:

当我尝试使用此循环生成列表时:

    List_attempt <- list()

levels<- levels(factor(Relationships$Parent))

for(n in 1:length(levels)){
  Children <- subset(Relationships, Relationships$Parent == levels[n], select = Child)
  for(c in 1:length(Children)){
    sublist <- as.list(Children)
    List_attempt <- list(List_attempt, name = levels[n],children = sublist)
  }
}

diagonalNetwork(List_attempt)
1) 是否有更好的方法为
对角网络
创建列表

2) 未能做到这一点;我如何修改循环以生成正确结构的列表

3) 我是否应该使用其他功能/软件包

谢谢你能给我的帮助,我已经在这堵墙上撞了一段时间了。我们也欢迎就SO提问的更好方式提供反馈

澄清:


在这里可以找到一个类似的问题。但是,它依赖于一个数据结构,其中根始终位于第一列,其子项位于后续列中,而不是像本问题中那样的边列表,这在igraph中常用

感谢您指出错误@symbolx

受@MrFlick comment的启发,建议从root开始,让child递归创建列表元素:)。。。当然,可以进一步提高对意外数据输入的鲁棒性

library(igraph)
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
    Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
net <- graph_from_data_frame(d=Relationships, directed=T)
plot(net)

#net and Relationships as declared in question
#get root
root <- setdiff(Relationships$Parent, Relationships$Child)

#traverse next layer and then recurve
as.list.igraph <- function(thisNode) {
    nm <- vertex_attr(net, "name", thisNode)
    childNodes <- V(net)[which(shortest.paths(net, thisNode, mode="out") == 1)]
    if (length(childNodes)==0) return(list(name=nm))
    list(name=nm, children=unname(lapply(childNodes, as.list.igraph)))
}

#plot D3 network
diagonalNetwork(as.list.igraph(V(net)[root]))
库(igraph)
库('networkD3')

关系您可以使用data.tree软件包,该软件包可以在开箱即用的情况下对分层数据进行许多转换:

library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
                           Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))

library('data.tree')
tree <- FromDataFrameNetwork(Relationships)
tree
lol <- ToListExplicit(tree, unname = TRUE)
diagonalNetwork(lol)
library('networkD3')

关系将不同的结构返回到发布在question@Symbolix很好,结构不同,但只是顺序不同。对于我的目标(图表),这不是一个问题。我将研究layout.reingold.tilford选项。这将非常有帮助,因为它允许我使用networkd3软件包的一些功能,而无需重复工作,现在我可以在一些事情上绘制igraph绘图。谢谢。@Delractiatus Maximus,我编辑它是为了澄清这两个问题之间的区别。一个具有相同的最终目标,但不转换关系数据,只有根在第一列,整个父子结构在后续列中时才有效,而在边列表中则不是这样。好的,我重新开始了这个问题
Error in FUN(X[[i]], ...) : 
  'options' must be a fully named list, or have no names (NULL)
library(igraph)
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
    Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
net <- graph_from_data_frame(d=Relationships, directed=T)
plot(net)

#net and Relationships as declared in question
#get root
root <- setdiff(Relationships$Parent, Relationships$Child)

#traverse next layer and then recurve
as.list.igraph <- function(thisNode) {
    nm <- vertex_attr(net, "name", thisNode)
    childNodes <- V(net)[which(shortest.paths(net, thisNode, mode="out") == 1)]
    if (length(childNodes)==0) return(list(name=nm))
    list(name=nm, children=unname(lapply(childNodes, as.list.igraph)))
}

#plot D3 network
diagonalNetwork(as.list.igraph(V(net)[root]))
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
                           Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))

library('data.tree')
tree <- FromDataFrameNetwork(Relationships)
tree
lol <- ToListExplicit(tree, unname = TRUE)
diagonalNetwork(lol)