One of the first steps to wrangling network data is to understand how it is strcutured. This section will walk you through the basic ways that network data is structured and demonstrate how to bring them into the R envrionment. During class time we will work on converting these data objects into network objects in R using the igraph package.

Keep in mind, you be using network data that is already stored into these formats or may be formatting original data. We will first discuss edgelists and then adjacency matrices.

Regardless of how your data is structred, the easiest way to store any network data is in a .csv excel file. We can use the read.csv() function in base R to read it into our environment.

Edgelists

Your data may be stored as an edgelist. An edgelist is what it says, a list of edges or relationships that exist between the nodes in your network. Since these are edges between nodes, the data are stored in a dyadic format (pairs).

Split across two columns you have the names of everyone in the network that share a connection. The basic format for any edgelist is to have a ‘from’ and a ‘to’ column. The titles of the columns are arbitrary, but are helpful for you as the researcher, especially if the connection is directed. You may wish to call the columns ‘sender’ and ‘receiver.’

This code chunk shows how to read in a .csv that is formatted as an edgelist. Note, the header = TRUE option tells R that the first row are headers (column names). Using the head() command, we see the first lines of these network data.

This is a network of romantic affiliations based on students from the Harry Potter saga. Note the column names reflect this.

PAUSE AND THINK: Is this a directed or an undirected network? What can you see to indicate whether it is or is not?

my_edge <- read.csv(file.choose(), header = TRUE)

head(my_edge)
##            Crusher            Crush
## 1     Harry Potter    Ginny Weasley
## 2     Harry Potter        Cho Chang
## 3      Ron Weasley Hermione Granger
## 4 Hermione Granger      Ron Weasley
## 5      Ron Weasley   Lavender Brown
## 6    Ginny Weasley     Harry Potter

Adjacecny Matrices

Your data may be stored as an adjacency matrix. An adjacency matrix is a datasheet that uses a numerical system (usually a binary system 0 and 1 for unweighted networks) to denote the ties that exist between cells in the spreadsheet. 0 indicates no tie and 1 indicates a tie. In a weigted network, the number may be higher than 1 (i.e. to indicate the number of interactions, the distance, or other weight).

The most important element of an adjacency matrix is that the first row and the first column have the list of nodes. Each cell is an individual node and this node is mirrored on the other side of the matrix. For example, cell A2 is the same as B1. These two lines (the first row and column) must have the same names in them in order for R to recognise it as a network. In other words, an adjacecny matrix has all the possible dyads (pairs) in the network with 1s and 0s to indicate whether they share a tie. Note that A1 should always be left empty.

One final characteristic of an adjacency matrix is the line where the same cell overlaps. This is called the diagonal. Cell A2 and B1 are the same name, the coordinates whether those cells meet (B2) can indicate whether that node is connected to itself. The same is true all the way down the diagonal of the matrix. The researcher (YOU) must decide whether self loops/ties make sense given the characteristics/parameters of the network when you collect network data. For example, in a network of sending text messages, it may not make sense.

This code chunk shows you how to bring in a .csv with network data stored as an adjacency matrix. These data are affiliation data between Harry Potter Characters based on their houses in Hogwarts. A 1 represents that they share a house-affiliation tie with the other character (i.e. they are in the same school house). Note, the row.names = 1 option is used here to ensure R recognises row 1 as none names not connections.

my_adj  <- read.csv(file.choose(), row.names=1) 

head(my_adj)
##                  Harry.Potter Draco.Malfoy Hermione.Granger Ron.Weasley
## Harry.Potter                0            0                1           1
## Draco.Malfoy                0            0                0           0
## Hermione.Granger            1            0                0           1
## Ron.Weasley                 1            0                1           0
## Ginny.Weasley               1            0                1           1
## Lily.Potter                 1            0                1           1
##                  Ginny.Weasley Lily.Potter James.Potter Severus.Snape
## Harry.Potter                 1           1            1             0
## Draco.Malfoy                 0           0            0             1
## Hermione.Granger             1           1            1             0
## Ron.Weasley                  1           1            1             0
## Ginny.Weasley                0           1            1             0
## Lily.Potter                  1           0            1             0
##                  Sirius.Black Lavender.Brown Nymphadora.Tonks Remus.Lupin
## Harry.Potter                1              1                0           1
## Draco.Malfoy                0              0                0           0
## Hermione.Granger            1              1                0           1
## Ron.Weasley                 1              1                0           1
## Ginny.Weasley               1              1                0           1
## Lily.Potter                 1              1                0           1
##                  Cho.Chang Cedric.Diggory Teddy.Lupin James.Sirius.Potter
## Harry.Potter             0              0           0                   1
## Draco.Malfoy             0              0           0                   0
## Hermione.Granger         0              0           0                   1
## Ron.Weasley              0              0           0                   1
## Ginny.Weasley            0              0           0                   1
## Lily.Potter              0              0           0                   1