繁体   English   中英

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

[英]Add missing mutual edges, while not changing attributes of existing mutual edges in R (igraph)

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

我需要填写所有缺少的倒数边(即,如果E(v2,v1) E(v1,v2)不存在E(v2,v1) ,我想创建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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM