jonzy
jonzy

Reputation: 11

merging a directed and undirected edgelist in r

I have the following networks (undirected and directed) that I would like to merge :

Undirected

   id1 id2 link1
1   1 984      0
2   1 883      1
3   1 941      0
4   1 650      0
5   1 132      1
6   1 601      0

Directed

   id1 id2
1   1  55
2   1 205
3   1 272
4   1 494
5   1 779
6   1 894

I would like to merge the links in network 1 and network 2 to obtain the following (undirected)

   id1 id2 link1 link2
1   1 984      0   1
2   1 883      1   0
3   1 941      1   0
4   1 650      0   1
5   1 132      1   1
6   1 601      0   1

I've tried to use as.undirected and then get.edge.attribute but i'm not getting the full list of observations to merge with the first network

Any idea on how to do it ?

Upvotes: 1

Views: 148

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48241

If I understand the problem correctly, then

gd$link2 <- 1
gm <- merge(gu, gd, all = TRUE)
gm[is.na(gm)] <- 0
gm
#    id1 id2 link1 link2
# 1    1  55     0     1
# 2    1 132     1     0
# 3    1 205     0     1
# 4    1 272     0     1
# 5    1 494     0     1
# 6    1 601     0     0
# 7    1 650     0     0
# 8    1 779     0     1
# 9    1 883     1     0
# 10   1 894     0     1
# 11   1 941     0     0
# 12   1 984     0     0

where gu is the undirected graph, gd is the directed one, and gm is the resulting one.

Upvotes: 1

Related Questions