Last update: 2026-02-21 19:30.

To download and open this file:

download.file("https://matteo.gagliolo.web.ulb.be/SOCAD460/Buxant_sna_initiation.r",
              "Buxant_sna_initiation.r")
file.edit("Buxant_sna_initiation.r")

Once you have opened the file, please insert all the commands to answer the exercises in the empty spaces below each exercise.

Please also provides comments in lines starting with # or #'.

Remember that this file should work as a standalone script, therefore all necessary packages and data should be created beforehand. As this is the first lecture, these commands are already provided here: next time you should be able to fill them in by yourselves.

Once you filled in the answers and tested their correctness, you can create an html version of this file, to be submitted on the UV along with the R version (this .r file). In order to do so, click on the notebook icon above the script in RStudio, or select Compile report in the File menu.

If compilation fails, try testing report compilation first. If compilation works with an empty template, then you have an issue with your code. It’s time to debug it! Open a new RStudio session, and test each line in order until you reproduce the error.

1 Example: Valente’s middleschool data

  1. Using indexation, create a smaller subset of the middleschool data, comprised only of the vertices 6, 7, 11, 34, and the corresponding edges.
    1. How many vertices and edges does this “subnetwork” include? 4 nodes (vertices) and 10 edges
      #nrow(ms_sub) = 4
      #sum(ms_sub) = 10
  2. Plot the subnetwork.
      #gplot(ms_sub, displaylabels = TRUE)
  3. Which relationships are _not_ reciprocated?
      #The one between 7 and 11 + between 7 and 34

library("UserNetR") # load the package accompanying the book
data("middleschool") 
library("sna")
Warning: le package 'sna' a été compilé avec la version R 4.2.3
Le chargement a nécessité le package : statnet.common
Warning: le package 'statnet.common' a été compilé avec la version R 4.2.3

Attachement du package : 'statnet.common'
Les objets suivants sont masqués depuis 'package:base':

    attr, order
Le chargement a nécessité le package : network
Warning: le package 'network' a été compilé avec la version R 4.2.3

'network' 1.18.2 (2023-12-04), part of the Statnet Project
* 'news(package="network")' for changes since last version
* 'citation("network")' for citation information
* 'https://statnet.org' for help, support, and other information
sna: Tools for Social Network Analysis
Version 2.7-2 created on 2023-12-05.
copyright (c) 2005, Carter T. Butts, University of California-Irvine
 For citation information, type citation("sna").
 Type help(package="sna") to get started.
library("network") # not needed as it is loaded by sna
ms_mat <- as.sociomatrix(middleschool)
# To create the submatrix I need to create an index via the c() function:
c(6, 7, 11, 34)
[1]  6  7 11 34
# The index can be used to extract the corresponding rows and columns of the original
# matrix. I store the result in a new variable, which I name ms_sub:
ms_sub <- ms_mat[c(6, 7, 11, 34), c(6, 7, 11, 34)]
# Here is the resulting matrix:
ms_sub
   6 7 11 34
6  0 1  1  1
7  1 0  0  0
11 1 1  0  1
34 1 1  1  0
# Notice how the row/column names of the full matrix have been preserved.

2 Example: Padgett’s Florentine families data

  1. Choose a subset of 4 families, then extract and plot the corresponding subnetwork.
# library commands are only needed once per session, however we replicate them
# here, in case you're only running portions of this file.
library("sna")
library("network") # not needed as it is loaded by sna
data("flo")

flo_sub <- flo[c("Castellani", "Pucci", "Barbadori", "Strozzi"), c("Castellani", "Pucci", "Barbadori", "Strozzi")]

gplot(flo_sub, displaylabels = TRUE)

3 Data formats

3.1 Matrices

  1. Create a matrix representing the network displayed on the right side of page 15 of the introduction.

3.2 Edge lists

  1. Create an edgelist representing the network displayed on the right side of page 15 of the introduction.

3.3 The network class

  1. Create a network object representing the network displayed on the right side of page 15 of the introduction.

  2. Convert flo to a network, keeping node labels.

3.4 Conversions

  1. Convert flo to an edgelist, keeping node labels.

Back to lectureIndex