To avoid mistakes and confusion,
it is better to detach the statnet package because there exist similar functions to the ones of the igraph package.
If we do not detach the statnet package,
we have to use the prefix network::
to specify that the function is from the network package
(which is included in the statnet package).
detach(package:statnet)
p_load(igraph)
Now, the starting point is the adjacency matrix again. This is equal to what has been done before.
adjacency_matrix <- rbind(
c(0,1,1,0,0),
c(0,0,1,1,0),
c(0,1,0,0,0),
c(0,0,0,0,0),
c(0,0,1,0,0)
)
Now that we have our matrix,
we can create a network object from it using the graph.adjacency()
function from the igraph package:
adjacency_network <- graph.adjacency(adjacency_matrix)
To confirm that our network object was created successfully, we can check its class type with the class()
function:
class(adjacency_network)
[1] "igraph"
We can also use the summary()
function to get more information about the network,
such as the number of vertices and edges:
summary(adjacency_network)
IGRAPH 50042ad DN-- 5 6 --
+ attr: name (v/c)
IGRAPH
: The first line always starts with IGRAPH, showing that the object is an igraph graph.
50042ad
: A seven character code is printed.
The code stands for the unique id of the graph but is not important to remember.
DN--
: A four letter long code string is printed.
The name
of the graph is printed if there is one, i.e. if the name graph attribute is set.
5 6
: The numbers denote the number of vertices and edges.
attr:name
: The attributes of the graph are listed, separated by a comma.
(v/c)
: The kind of attribute - graph (“g”), vertex (“v”) of edge (“e”) is denoted and
the type of the attribute as well, character (“c”), numeric (“n”), logical (“l”) or other (“x”)
A network is created using igraph.
The output of the summary()
function returns
53ecc7b UN-B 8 12 -- attr: name (e/n)
as output.
Assign the correct answer to the MC variable (1, 2, 3 or 4).