In this exercise, we will learn how to add attributes to the nodes of a network using the statnet package in R. Node attributes can provide additional information about the nodes in a network, such as their gender, age, or degree.
Suppose we have the network adjacency_network
from
exercise 2.11
and we have additional information about the gender of the network members.
We can add this information as a node attribute using the set.vertex.attribute()
function:
network::set.vertex.attribute(adjacency_network, "gender", c("F", "F","M", "F", "M"))
The network::
prefix is used to specify that the function is from the network package.
We can also add the degree of each node as an attribute.
The degree of a node is the number of edges connected to it.
Here’s how to add the degree as an attribute called alldeg
:
adjacency_network %v% "alldeg" <- sna::degree(adjacency_network)
We can check the attributes of a network using the list.vertex.attributes()
function:
list.vertex.attributes(adjacency_network)
[1] "alldeg" "gender" "na" "vertex.names"
We can inspect the values of an attribute in two ways.
The first way is using the get.vertex.attribute()
function:
get.vertex.attribute(net1, "gender")
"F" "F" "M" "F" "M"
The second way is using the %v%
operator:
net1 %v% "alldeg"
[1] 2 4 4 1 1
In exercise 2.1, you created the network friends_network
.
Assume there is additional information about whether a person speaks Dutch, "D" or French, "F".
Person 1 and 4 speak Dutch, while person 2, 3 and 5 speak French.
Create an additional attribute called 'language'.
Assume that:
friends_network
, as created before, has been loaded.