5.2 Adjacency matrices (recommended structure)
<- matrix(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0), nrow = 5, ncol = 5, dimnames = list(c("Mark", "Peter", "Bob", "Jill", "Aaron"), c("Mark", "Peter", "Bob", "Jill", "Aaron")))
adjacency
print(adjacency)
## Mark Peter Bob Jill Aaron
## Mark 0 1 0 1 0
## Peter 1 0 1 0 1
## Bob 0 1 0 1 0
## Jill 1 0 1 0 1
## Aaron 0 1 0 1 0
Pros:
- More efficient than edge lists (Example: searching connections).
Cons:
- More suited form computers than human (Example: difficult to record).
Convert edge list to adjacency matrix
%>%
edgelist as.matrix() %>%
graph_from_edgelist(directed = FALSE) %>%
as_adjacency_matrix(sparse = TRUE)
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Mark Peter Jill Bob Aaron
## Mark . 1 1 . .
## Peter 1 . . 1 1
## Jill 1 . . 1 1
## Bob . 1 1 . .
## Aaron . 1 1 . .