Change the matrix to get the network

I need to convert a matrix from two columns to a matrix similar to the adjacency matrix. I have the following dataset:

firm_id_1 firm_id_2 1 2 1 4 1 5 2 1 2 3 3 2 3 6 4 1 4 5 5 4 6 3 

etc .... for 4000 different firm_id .

In the first column, there is a direct relationship between firm_id_1 and firm_id_2 . For example, firm_id = 1 is directly connected (first degree) with firm_id 2, 4, 5, indirectly connected (with second degree) with 3 through firm_id = 2 and indirectly connected with third degree firm_id = 6 through firm_id = 3, etc. ...

I would like to build this matrix in R:

 firm_id [1] [2] [3] [4] [5] [6] [1] 1 2 1 1 3 [2] 1 1 2 [3] 1 1 [4] 1 [5] 1 [6] 1 

Each number represents the degree of separation in the network. Is this doable in R?

+6
source share
1 answer
 library(igraph) g <- graph.edgelist(mat) shortest.paths(g) # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] 0 1 2 1 1 3 # [2,] 1 0 1 2 2 2 # [3,] 2 1 0 3 3 1 # [4,] 1 2 3 0 1 4 # [5,] 1 2 3 1 0 4 # [6,] 3 2 1 4 4 0 
+8
source

All Articles