Starting from the Edge List

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.

Generating Data

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)
)

Creating a Network Object

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")

Naming Nodes

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")

Converting Between Representations

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().

Multiple choice

In the previous exercise, you created the friends_network network. What object type will as.sociomatrix(friends_network) result in?

  1. Adjancency Matrix
  2. Edge List
  3. Network Object

Answer by assigning the correct number (1, 2 or 3) to the MC variable.