Reputation: 1278
I have a weighted edge list and I want 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 Weight
1 a a 0.3274570
2 b a 0.7188648
3 a b 0.1097450
4 b b 0.9054419
Here is replication code:
edgelist <- setNames(cbind(expand.grid(letters[1:2], letters[1:2]), runif(4)), c("From", "To", "Weight"))
edgelist
Upvotes: 1
Views: 1659
Reputation: 24770
One approach is to use get.adjacency
from the igraph
package.
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
While it is certainly possible to simply transform the data to a matrix, it might be helpful to be prepared to use the rest of this package's features.
Upvotes: 3