I have a directed affiliation matrix which I want to convert to an edge list. The matrix looks like this:
State WarID Initiator
A 1 1
B 1 0
A 2 1
C 2 0
D 2 0
B 3 1
C 3 1
D 3 0
where "State" is the name of a country, "WarID" is a unique identifier for war, and "Initiator" is a dummy variable which equals 1 if the state initiated the war. There is an edge between two states if they share the same "WarID" but have different value of "Initiator."
I want to change the affiliation matrix above into an edge list like this:
Initiator Target WarID
A B 1
A C 2
A D 2
B D 3
C D 3
I know how to change a basic affiliation matrix into an edge list, but I struggled with keeping the "directed network" component. I'll be very grateful if someone could tell me how to do this in R efficiently (I have a pretty large affiliation matrix).
You could group the data by
WarID
andInitiator
usingtapply
and make anexpand.grid
for eachWarID
. Justrbind
the results.Notice that I used consecutive
WarID
s as specified by you.Data: