Starting from the Edge List with igraph

In this exercise, we will learn how to create a network object from an edge list using the igraph package in R.

Generating Data

First, we need to generate some data in the form of an edge 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 graph.edgelist() function from the igraph package:

edge_network <- graph.edgelist(edge_list)

Setting Node and Edge Attributes In the igraph package, we use the V() and E() functions to access and set vertex and edge attributes, respectively. Here’s how to set the node names and assign a value to each edge:

V(inet2)$name <- c("A", "B", "C", "D", "E")
E(inet2)$val <- c(1:6)

Switching Between Packages

It’s also possible to switch between the igraph package and the network package using the intergraph package. Here’s how to convert an igraph object to a network object and vice versa:

p_load(intergraph)
# Convert igraph object to network object
edge_network <- asNetwork(edge_network)
# Convert network object to igraph object
edge_network <- asIgraph(edge_network)

Multiple choice

The intergraph package in R is used to convert network objects between different formats. Which of the following statements about the intergraph package is correct? Assign 1, 2, 3 or 4, to the MC variable depending on which of the four answers is correct.

  1. The intergraph package can only convert igraph objects to network objects.
  2. The intergraph package can convert both igraph objects to network objects and network objects to igraph objects.
  3. The intergraph package can only convert network objects to igraph objects.
  4. The intergraph package cannot convert between different network object formats.