Get startedGet started for free

Adjacency matrices

In this exercise, you will extract and compute the first and second order adjacency matrices of the network.
You've already seen how to extract the first order adjacency matrix using the as_adjaceny_matrix() function in the slides. For the second-order adjacency matrix, you need to multiply the first order matrix with itself and replace all the positive values with 1 since we are working with unweighted networks only. You also need to make sure the elements on the diagonal are 0 since we do not allow self-edges.

This exercise is part of the course

Predictive Analytics using Networked Data in R

View Course

Exercise instructions

  • Extract the network's adjacency matrix using the as_adjacency_matrix() function. Name the matrix AdjacencyMatrix.
  • Compute the second order adjacency matrix by multiplying AdjacencyMatrix with itself and call it SecondOrderMatrix_adj.
  • Create a new matrix, SecondOrderMatrix, by conditioning on SecondOrderMatrix_adj to make all positive values equal to 1. The elements on the diagonal should be 0.
  • Inspect the first 10 rows and first 10 columns of SecondOrderMatrix.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Extract the adjacency matrix
AdjacencyMatrix <- as_adjacency_matrix(___)

# Compute the second order matrix
SecondOrderMatrix_adj <- ___ %*% ___

# Adjust the second order matrix
SecondOrderMatrix <- ((___) > 0) + 0
diag(SecondOrderMatrix) <- 0

# Inspect the second order matrix
SecondOrderMatrix[___:___, ___:___]
Edit and Run Code