Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将edgelist转换为data.tree_R_Dataframe_Tree - Fatal编程技术网

将edgelist转换为data.tree

将edgelist转换为data.tree,r,dataframe,tree,R,Dataframe,Tree,我想使用R中的data.tree库将edgelist转换为树对象 这是我的代码: library(data.tree) edges <- read.csv("data.csv") colnames(edges) <- c("source", "target") R1 <- data.frame(Parent=edges$source, Child=edges$target) R2<- data.frame(Parent=c("earth","earth","for

我想使用R中的
data.tree
库将edgelist转换为树对象

这是我的代码:

library(data.tree)

edges <- read.csv("data.csv")
colnames(edges) <- c("source", "target") 

R1 <- data.frame(Parent=edges$source, Child=edges$target)

R2<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
                           Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
这怎么可能

R1看起来像这样:

  Parent Child
1      1     6
2      4     2
3      3     5
4      1     4
5      1     5
6      5     3
  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
R2看起来像这样:

  Parent Child
1      1     6
2      4     2
3      3     5
4      1     4
5      1     5
6      5     3
  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

正如@lukeA所说,R1包含一个循环关系。如果3是5的父级,而5是3的父级,
FromDataFrameNetwork(R1)
将在尝试构建树时无限递归。您可以编写递归帮助函数来验证要转换为树的表,例如:

library(tidyverse)

validate <- function(origin, tree, ancestors = character()){
  ancestors = c(ancestors, origin)

  kids <- tree %>%
    filter(Parent == origin) %>%
    select(Child) %>%
    pull()

  invalid.kids <- kids[kids %in% ancestors] # if any Child appears in its own ancestry, then it's invalid

  if(length(invalid.kids)){
    print(paste('invalid children:',
    paste(invalid.kids, collapse = ', '),
    'parent:',
    origin)) # print Children that produce circular relationships for debugging

  }

  children <- lapply(kids[!(kids %in% ancestors)], validate, tree, ancestors) # recursively apply validate() to all valid Children

  tree <- filter(tree, !(Child %in% invalid.kids)) # key step: filter out parts that would cause an infinite loop

  return(rbind(tree, do.call(rbind, children)))
}
库(tidyverse)
验证%
选择(子项)%>%
拉

无效。正如@lukeA所说,R1包含循环关系。如果3是5的父级,而5是3的父级,
FromDataFrameNetwork(R1)
将在尝试构建树时无限递归。您可以编写递归帮助函数来验证要转换为树的表,例如:

library(tidyverse)

validate <- function(origin, tree, ancestors = character()){
  ancestors = c(ancestors, origin)

  kids <- tree %>%
    filter(Parent == origin) %>%
    select(Child) %>%
    pull()

  invalid.kids <- kids[kids %in% ancestors] # if any Child appears in its own ancestry, then it's invalid

  if(length(invalid.kids)){
    print(paste('invalid children:',
    paste(invalid.kids, collapse = ', '),
    'parent:',
    origin)) # print Children that produce circular relationships for debugging

  }

  children <- lapply(kids[!(kids %in% ancestors)], validate, tree, ancestors) # recursively apply validate() to all valid Children

  tree <- filter(tree, !(Child %in% invalid.kids)) # key step: filter out parts that would cause an infinite loop

  return(rbind(tree, do.call(rbind, children)))
}
库(tidyverse)
验证%
选择(子项)%>%
拉

无效。R1 3中的孩子是5的家长,5是3的家长。这怎么可能在树中呢?在R1中,3是5的父级,5是3的父级。这怎么可能在树上?