添加缺少的相互边缘,同时不更改 R 中现有相互边缘的属性 (igraph)



我有一个有向图,G。 G 中的一些边是互易的,有些则不是。 对于互易边,边属性可能不同。 也就是说E(v1,v2)$att可能不等于E(v2,v1)$att

我需要填写所有缺失的倒数边(也就是说,如果E(v2,v1)不存在而E(v1,v2)不存在,我想创建E(v2, v1)并从E(v1, v2)复制所有属性信息(。

如果倒数边确实存在,我需要保留唯一的边属性信息。

有很多边缘和很多属性,所以我试图避免在这里循环。 目前,其中g1是有向但不完整的图,我:

#make undirected with loops for reciprocal friendships
g2 <- as.undirected(g1, mode = c("each"))
#force everything to be directed, create new edges
g <- as.directed(g2, mode = c("mutual"))
#get rid of the double loops. 
gnew <- simplify(g, remove.multiple = TRUE,
edge.attr.comb = "random")   

唯一的问题是edge.attr.comb = "random"也就是说,我覆盖了预先存在的相互边缘属性信息。 我认为我可以标记g1中缺少的相互边缘并使用which_mutual添加必要的边缘(并复制其属性信息(,但是在边缘索引方面遇到了困难。 我一定错过了一个简单的解决方案。举个例子:

g <- graph_from_literal(A+-+B, A-+C)
E(g)$att1 <- c(1,2,3)
#I want (note the default order of vertices for igraph)
g2 <- graph_from_literal(A+-+B, A+-+C)
E(g2)$att1 <- c(1, 2, 3, 2) 

想通了。 也许不是最雄辩的解决方案,但它有效。 举个例子,

g <- graph_from_literal(10-+40, 10-+30, 20+-+40)
E(g)$att1 <- c(1,2,3, 4)
E(g)$att2 <- c(10, 11, 12, 13)
######################################################################
test <- which((which_mutual(g) == FALSE))
head <- head_of(g,test)
tail <- tail_of(g,test)
combine <- matrix(c(head,tail), ncol = length(test), byrow = TRUE)
combineV <- as.vector(combine)
attributes <- list(att1 = E(g)$att1[test],att2 = E(g)$att2[test])
gnew <- add_edges(g,combineV, attr = attributes)

最新更新