3
votes

I have the following edge list with a number that associates the edge with a path number. This is given by the following matrix which I call Totallist: `

Begin edge  end edge  path number
1               3           1
3               4           1
4               5           1
6               3           2
3               2           2`

I want to construct adjacency matrices for each of the paths. In this example, I want two matrices, but there could be more. I have written the following but it only finds the matrix for the first path. I am unsure how to write something that will work for any number of paths that I throw at it:

 X<-as.data.frame(table(Totallist[,3]))

nlines<-nrow(X)
nlines
freq<-X[1,2]

diameterofmatrix<-max(Totallist)

X1<-get.adjacency(graph.edgelist(as.matrix(Totallist[1:X[1,2],1:2]), directed=FALSE))
X1<-rbind(X1, 0)
X1<-cbind(X1, 0)
X1

I also need the matrices to all be the same dimension so that is why I added an extra row and column. I could continue using my method but it seems quite ugly. Thank you very much for any help.

1

1 Answers

1
votes

To extract the adjacency matrices into a list you can do the following (I generate some fake data):

set.seed(42)
df <- data.frame(beginEdge = sample(1:10, 10, replace = TRUE), 
                 endEdge = sample(1:10, 10, replace=TRUE), 
                 pathNum = rep(c(1,2), each=5))
df

      beginEdge endEdge pathNum
1         10       5       1
2         10       8       1
3          3      10       1
4          9       3       1
5          7       5       1
6          6      10       2
7          8      10       2
8          2       2       2
9          7       5       2
10         8       6       2


paths <- unique(df$pathNum) # get the paths to iterate through

If we make the nodes factors, and set the levels of the factors to all the nodes in the population, then the adjacency matrices will be computed for the population in your network. I am assuming here the network is ten actors. If your observed data contains all the nodes you want to work with set the levels to unique(c(df$beginEdge,df$endEdge)), or whatever the set of nodes are that you prefer.

df$beginEdge <- factor(df$beginEdge, levels=1:10) 
df$endEdge <- factor(df$endEdge, levels=1:10)

We now go across the list of paths and create matrices storing them as a list:

  list.of.adj.mats <- lapply(paths, function(i){
      matrix(as.numeric((
      table(df$beginEdge[df$pathNum==i],
        df$endEdge[df$pathNum==i])+
      table(df$endEdge[df$pathNum==i],
      df$beginEdge[df$pathNum==i]))>0), 
  nrow=length(levels(df$beginEdge)))})
list.of.adj.mats
[[1]]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    0    0    0    0    0    0    0    1     1
 [4,]    0    0    0    0    0    0    0    0    0     0
 [5,]    0    0    0    0    0    0    1    0    0     1
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    0    0    0    0    1    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     1
 [9,]    0    0    1    0    0    0    0    0    0     0
[10,]    0    0    1    0    1    0    0    1    0     0

[[2]]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    0    0    0    0    0    0    0     0
 [4,]    0    0    0    0    0    0    0    0    0     0
 [5,]    0    0    0    0    0    0    1    0    0     0
 [6,]    0    0    0    0    0    0    0    1    0     1
 [7,]    0    0    0    0    1    0    0    0    0     0
 [8,]    0    0    0    0    0    1    0    0    0     1
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    1    0    1    0     0