Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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
Graph.union求和边权重属性(igraph R)_R_Igraph - Fatal编程技术网

Graph.union求和边权重属性(igraph R)

Graph.union求和边权重属性(igraph R),r,igraph,R,Igraph,我有两张带有重量标签的图表: library(igraph) g1= graph.formula(A -+ B, A -+ C) E(g1)["A" %->% "B"]$weight= 1 E(g1)["A" %->% "C"]$weight= 2 E(g1)$label= E(g1)$weight g2= graph.formula(A -+ B, A -+ C, A -+ D) E(g2)["A" %->% "B"]$weight= 10 E(g2)["A" %->

我有两张带有重量标签的图表:

library(igraph)

g1= graph.formula(A -+ B, A -+ C)
E(g1)["A" %->% "B"]$weight= 1
E(g1)["A" %->% "C"]$weight= 2
E(g1)$label= E(g1)$weight

g2= graph.formula(A -+ B, A -+ C, A -+ D)
E(g2)["A" %->% "B"]$weight= 10
E(g2)["A" %->% "C"]$weight= 20
E(g2)["A" %->% "D"]$weight= 100
E(g2)$label= E(g2)$weight

par(mfrow= c(2,1), mar= rep(0,4))
plot(g1); plot(g2)

使用
graph.union()
连接这两个属性时,
igraph
默认情况下会创建
weight_1、weight_2
属性

问题:

我希望连接的图具有求和的边权重属性。应用现有的不是最优的。 首先,解决方案在case
graph.union()
中不能很好地伸缩。union()会创建更多的
weight\uux…
属性。其次,在可再现示例的情况下,它只导致部分解决方案,因为边缘
“a”“D”
不包含和

g= graph.union(g1, g2)
E(g)$weight= E(g)$weight_1 + E(g)$weight_2
E(g)$label= E(g)$weight

问题:

如何重新编码以最终获得以下图形:


注释:我不是在寻找手动解决方案(
E(g)[“a”%->%“D”]$label=100
),因为我正在处理大量边缘。

根据Gabor的建议:

library(igraph)
library(intergraph)
library(dplyr)

# helper function
as.data.frame.igraph= function(g) {
  # prepare data frame
  res= cbind(as.data.frame(get.edgelist(g)),
             asDF(g)$edges)[ , c(-3, -4)]
  # unfactorize
  res$V1= as.character(res$V1)
  res$V2= as.character(res$V2)
  # return df
  res
}

df_g1= as.data.frame(g1)
df_g2= as.data.frame(g2)
df= rbind_all(list(df_g1, df_g2)) %>%
  group_by(V1, V2) %>%
  summarise(weight= sum(weight))

new_graph= simplify(graph.data.frame(df, directed = T))
E(new_graph)$weight=  df$weight
E(new_graph)$label= E(new_graph)$weight

plot(new_graph)

您签出属性组合了吗?有一个选项
sum
对您有效。我知道这些选项,但我不认为它们对graph.union有什么帮助。如果你有一个解决方案,请将它作为答案发布在可重复的示例上。我认为最简单的方法是将图形转换为数据帧,进行连接,然后将结果转换回图形。