In this exercise, we will learn how to create a network object from an edge list using the statnet package in R. This is another common way to represent networks in data analysis.
First, we need to generate some data in the form of an edge list. This list will represent our network, where each row corresponds to an edge, and the two values in each row represent the nodes that are connected by that edge.
Here’s an example of how to generate such a list:
edge_list <- rbind(
c(1,2),
c(1,3),
c(2,3),
c(2,4),
c(3,2),
c(5,3)
)
Now that we have our edge list, we can create a network object from it using the network()
function.
Since we’re using an edge list as input, we specify this with the matrix.type
parameter:
edge_network <- network(edge_list, matrix.type = "edgelist")
Since the names for the nodes are not yet provided, they need to be defined in the network object itself:
network.vertex.names(edge_network) <- c("A", "B", "C", "D", "E")
It’s also possible to switch the network object back to the adjacency matrix
and vice versa using the functions as.sociomatrix()
and as.matrix.network.edgelist()
.
In the previous exercise, you created the friends_network
network.
What object type will as.sociomatrix(friends_network)
result in?
Answer by assigning the correct number (1, 2 or 3) to the MC
variable.