Back
I am having an edge list and I just need to convert it into an adjacency matrix. Is there a simple code I can use to do this?
The data looks like this:
From To Weight1 a a 0.32745702 b a 0.71886483 a b 0.10974504 b b 0.9054419
From To Weight
1 a a 0.3274570
2 b a 0.7188648
3 a b 0.1097450
4 b b 0.9054419
Here is the code, I have implemented:
edgelist <- setNames(cbind(expand.grid(letters[1:2], letters[1:2]), runif(4)), c("From", "To", "Weight"))edgelist
edgelist <- setNames(cbind(expand.grid(letters[1:2], letters[1:2]), runif(4)), c("From", "To", "Weight"))
edgelist
In your case what you can do is use the get.adjacency() from the igraph package in R programming.
library(igraph)mygraph <- graph.data.frame(edgelist)get.adjacency(mygraph, sparse = FALSE, attr='Weight')# a b#a 0.3274570 0.1097450#b 0.7188648 0.9054419
library(igraph)
mygraph <- graph.data.frame(edgelist)
get.adjacency(mygraph, sparse = FALSE, attr='Weight')
# a b
#a 0.3274570 0.1097450
#b 0.7188648 0.9054419
You can simply transform the data to a matrix, it might be helpful to be prepared to use the rest of this package's features.
31k questions
32.8k answers
501 comments
693 users