Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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
如何删除数据帧中的子元素以绘制R中的网络?_R_Igraph - Fatal编程技术网

如何删除数据帧中的子元素以绘制R中的网络?

如何删除数据帧中的子元素以绘制R中的网络?,r,igraph,R,Igraph,我想用R绘制一个网络。然而,问题是,我的数据框(其中包含关系数据)也包含子对象的子对象。如下所示: parent <- c("A","A","A","B","B","E") child <- c("B","C","D","C","D","D") df <- data.frame(parent,child) 使用igraph来确定个体之间的路径如何,如果有多个路径,请删除该连接 library(igraph) parent <- c("A","A","A","B","B

我想用R绘制一个网络。然而,问题是,我的数据框(其中包含关系数据)也包含子对象的子对象。如下所示:

parent <- c("A","A","A","B","B","E")
child <-  c("B","C","D","C","D","D")
df <- data.frame(parent,child)

使用igraph来确定个体之间的路径如何,如果有多个路径,请删除该连接

library(igraph)
parent <- c("A","A","A","B","B","E")
child <-  c("B","C","D","C","D","D")
df <- data.frame(parent,child)
net <- graph_from_data_frame(df,directed = T)
df <- df[apply(df,1,function(x){length(all_simple_paths(net,x[1],x[2]))}) == 1,]
df
  parent child
1      A     B
4      B     C
5      B     D
6      E     D
库(igraph)

父级这是一种
dplyr
解决方案:

library(dplyr)

# get data
parent <- c("A","A","A","B","B","E")
child <-  c("B","C","D","C","D","D")
df <- data.frame(parent,child, stringsAsFactors = FALSE)

# remove rows that are not directly related
new_df <- anti_join(df,
          left_join(df,df,by=c("child"="parent")) %>% 
  select(parent,child=child.y) %>% 
  na.omit()) 

new_df
  parent child
1      A     B
2      B     C
3      B     D
4      E     D
库(dplyr)
#获取数据

家长我理解的对吗?您想保留家长中不在孩子中的所有项目?不,最后一部分只是一个想法,最终没有成功。我想从df中删除不直接相关的行。例如,保留第一行(AB),但删除第二行(AC)和第三行(AD),因为A与C和D没有直接关系,而只是通过B。因为B(即A的子级)也将C和D作为子级。这是分层模型的一个前提条件,因此a与C和D没有直接关系。如果它这么简单,那就太酷了!但是,我收到了一条错误消息:“as.igraph.vs(graph,from)中的错误:无效的顶点名称”:(抱歉,我遗漏了一个重要步骤。谢谢!!!效果非常好!我从未想过这一点。我尝试了一些数据,但速度一点也不慢。所以对我来说,这很好。
`%notin%` <- Negate(`%in%`)
i <- nrow(df)
y <- list()
z <- list()
j <- 1
while (i > 0) {
  v <- unique(df$parent[!(df$parent %in% df$child)]) # find mismatch (only in parent, not in child)
  df <- df %>% filter(parent %notin% v)
  print(v)
  y[[j]] <- v
  z[[j]] <- df
  i = nrow(df)
  j = j+1
}
library(igraph)
parent <- c("A","A","A","B","B","E")
child <-  c("B","C","D","C","D","D")
df <- data.frame(parent,child)
net <- graph_from_data_frame(df,directed = T)
df <- df[apply(df,1,function(x){length(all_simple_paths(net,x[1],x[2]))}) == 1,]
df
  parent child
1      A     B
4      B     C
5      B     D
6      E     D
library(dplyr)

# get data
parent <- c("A","A","A","B","B","E")
child <-  c("B","C","D","C","D","D")
df <- data.frame(parent,child, stringsAsFactors = FALSE)

# remove rows that are not directly related
new_df <- anti_join(df,
          left_join(df,df,by=c("child"="parent")) %>% 
  select(parent,child=child.y) %>% 
  na.omit()) 

new_df
  parent child
1      A     B
2      B     C
3      B     D
4      E     D