1
votes

I would like to transform some edge lists into adjacency matrix. However, I face various complications: My data set consists of 27 actors who may or may not have a tie (weighted between 1-5, and directed) over 3 moments in time.

This means, for instance, that for t=1, I have an edge list like this:

countryA,countryB,tie
AUH,GMY,2
AUH,RUS,1
AUH,UKG,4
BOL,PER,5
BRA,ARG,1
PAR,UKG,4

And for t=2, I have an edge list like this:

countryA,countryB,tie
ARG,AUH,1
AUH,UKG,4
BOL,PER,5
BRA,ARG,1
PAR,UKG,4
RUS,UKG,2

The problem is that for each period I would like to have a matrix of 27x27. This means that also all the actors which may not share a tie with anyone else are included for every period of time. Basically, I would like to have something like this for all 3 periods (note that, ideally, the commas are gone):

0 0 0 3 2 1 4 
0 0 4 2 1 0 0
0 0 0 0 3 4 4 
0 0 0 0 2 1 4 
0 0 4 2 0 0 0
0 0 0 0 3 0 4 
0 3 4 2 0 0 0 

Of course I already checked various forum posts, help sites and the like. But I wasn't able to figure out how I should do this. I am frustrated.

1

1 Answers

1
votes

Let's assume you have your data.frames stored in a list. Here's some sample data

t1<-structure(list(countryA = structure(c(1L, 1L, 1L, 2L, 3L, 4L), .Label = c("AUH", 
"BOL", "BRA", "PAR"), class = "factor"), countryB = structure(c(2L, 
4L, 5L, 3L, 1L, 5L), .Label = c("ARG", "GMY", "PER", "RUS", "UKG"
), class = "factor"), tie = c(2L, 1L, 4L, 5L, 1L, 4L)), .Names = c("countryA", 
"countryB", "tie"), class = "data.frame", row.names = c(NA, -6L
))
t2<-structure(list(countryA = structure(1:6, .Label = c("ARG", "AUH", 
"BOL", "BRA", "PAR", "RUS"), class = "factor"), countryB = structure(c(2L, 
4L, 3L, 1L, 4L, 4L), .Label = c("ARG", "AUH", "PER", "UKG"), class = "factor"), 
    tie = c(1L, 4L, 5L, 1L, 4L, 2L)), .Names = c("countryA", 
"countryB", "tie"), class = "data.frame", row.names = c(NA, -6L
))

tt<-list(t1=t1, t2=t2)

First, you want to get a list of all unique country names

countries <- sort(unique(unlist(sapply(tt, function(x) sapply(x[,1:2], levels)))))

Now, you want to make sure all of your data.frames use the same factor levels for the country columns. We can do that with

ttx <- lapply(tt, function(x) {x[,1:2]<-lapply(x[,1:2], factor, levels= countries); x})

Now that they are all aware of all the countries, we can use xtabs to create the adjacency matrix with weighted tie values

lapply(ttx, function(x) xtabs(tie~countryA+countryB, x))

which results in

$t1
        countryB
countryA ARG AUH BOL BRA GMY PAR PER RUS UKG
     ARG   0   0   0   0   0   0   0   0   0
     AUH   0   0   0   0   2   0   0   1   4
     BOL   0   0   0   0   0   0   5   0   0
     BRA   1   0   0   0   0   0   0   0   0
     GMY   0   0   0   0   0   0   0   0   0
     PAR   0   0   0   0   0   0   0   0   4
     PER   0   0   0   0   0   0   0   0   0
     RUS   0   0   0   0   0   0   0   0   0
     UKG   0   0   0   0   0   0   0   0   0

$t2
        countryB
countryA ARG AUH BOL BRA GMY PAR PER RUS UKG
     ARG   0   1   0   0   0   0   0   0   0
     AUH   0   0   0   0   0   0   0   0   4
     BOL   0   0   0   0   0   0   5   0   0
     BRA   1   0   0   0   0   0   0   0   0
     GMY   0   0   0   0   0   0   0   0   0
     PAR   0   0   0   0   0   0   0   0   4
     PER   0   0   0   0   0   0   0   0   0
     RUS   0   0   0   0   0   0   0   0   2
     UKG   0   0   0   0   0   0   0   0   0

which you can then do whatever you like with. It's just important to get them all using the same factor levels to get a tables with the same dimensions even if some observations are missing.