Get startedGet started for free

Neighbors

Often in network analysis it is important to explore the patterning of connections that exist between vertices. One way is to identify neighboring vertices of each vertex. You can then determine which neighboring vertices are shared even by unconnected vertices indicating how two vertices may have an indirect relationship through others. In this exercise you will learn how to identify neighbors and shared neighbors between pairs of vertices.

This exercise is part of the course

Network Analysis in R

View Course

Exercise instructions

  • Using the function neighbors() identify the vertices that are connected in any manner to vertex 12, those vertices that direct an edge to vertex 12 and those vertices that receive a directed edge from vertex 12. This can be achieved by choosing the correct value in the argument mode. Choose from all, in and out.
  • Determine if vertices 42 and 124 have a neighbor in common. Create a vector n1 of those vertices that receive an edge from vertex 42 and a vector n2 of those vertices that direct an edge to vertex 124 using neighbors(). Next use intersection() to identify if there are any vertices that exist in both n1 and n2.

Hands-on interactive exercise

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

library(igraph)

# Identify all neighbors of vertex 12 regardless of direction
___(g, '12', mode = c('___'))

# Identify other vertices that direct edges towards vertex 12
___(g, '12', mode = c('___'))

# Identify any vertices that receive an edge from vertex 42 and direct an edge to vertex 124
n1 <- ___(g, '___', mode = c('___'))
n2 <- ___(g, '___', mode = c('___'))
___(n1, n2)
Edit and Run Code