I am trying to merge two graphs in R, using igraph. Ideally, I would create a union of vertices from g1
and g2
, keeping edges from g1
only. This union should be created based on the label
attribute. I guess I could simply remove all the edges from g2
before merging, using something like this:
g2 %>% delete_edges(seq(1, length(E(g2)), by = 1))
Still, when I create a union like this:
g.union <- graph.union(g1, g2, byname=F)
I get a graph with attributes id_1
, id_2
, label_1
, label_2
, weight_1
, weight_2
... Which is not quite what I want. I need to retain all the vertices and edges from g1
adding only those vertices from g2
that are missing in g1
. Keeping all properties of those added vertices.
Any help appreciated!
EDIT:
@MrFlick, I can't share those graphs, but a simple example would be something like this:
I have g1
graph
[
directed 1
node
[
id 1
label "it2igcryfm862x"
mydetails "somedetails1"
]
node
[
id 2
label "it0l2xa53eu1w3"
mydetails "somedetails2"
]
node
[
id 3
label "iszyxcopnao380"
mydetails "somedetails3"
]
edge
[
source 1
target 2
weight 1
]
edge
[
source 1
target 3
weight 2
]
edge
[
source 2
target 3
weight 1
]
]
and g2
graph
[
directed 1
node
[
id 1
label "it2igcryfm862x"
mydetails "somedetails1"
]
node
[
id 2
label "it0l2xa53eu1w3"
mydetails "somedetails2"
]
node
[
id 3
label "iszyxcopnao380"
mydetails "somedetails3"
]
node
[
id 4
label "it0lhztmkln4n6"
mydetails "somedetails4"
]
edge
[
source 1
target 2
weight 1
]
edge
[
source 1
target 3
weight 3
]
edge
[
source 2
target 3
weight 2
]
edge
[
source 2
target 4
weight 2
]
edge
[
source 3
target 4
weight 1
]
]
and what I need is g3
graph
[
directed 1
node
[
id 1
label "it2igcryfm862x"
mydetails "somedetails1"
]
node
[
id 2
label "it0l2xa53eu1w3"
mydetails "somedetails2"
]
node
[
id 3
label "iszyxcopnao380"
mydetails "somedetails3"
]
node
[
id 4
label "it0lhztmkln4n6"
mydetails "somedetails4"
]
edge
[
source 1
target 2
weight 1
]
edge
[
source 1
target 3
weight 2
]
edge
[
source 2
target 3
weight 1
]
]