Network Analysis Exercises in R: 15 Practice Problems

Fifteen practice problems on network analysis in R with igraph: degrees, centrality, communities, paths, viz.

RRun this once before any exercise
library(igraph)

  

Exercise 1: Create graph from edges

Difficulty: Beginner.

Show solution
RInteractive R
graph_from_literal(A-B, B-C, C-D, A-D)

  

Exercise 2: Number of nodes/edges

Difficulty: Beginner.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D) list(vcount = vcount(g), ecount = ecount(g))

  

Exercise 3: Degree

Difficulty: Beginner.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D) degree(g)

  

Exercise 4: Adjacency matrix

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D) as_adjacency_matrix(g)

  

Exercise 5: Plot

Difficulty: Beginner.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D) plot(g)

  

Exercise 6: Directed graph

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-+B, B-+C) plot(g)

  

Exercise 7: Betweenness centrality

Difficulty: Advanced.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D, B-D) betweenness(g)

  

Exercise 8: Closeness

Difficulty: Advanced.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D) closeness(g)

  

Exercise 9: Eigenvector centrality

Difficulty: Advanced.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D) eigen_centrality(g)$vector

  

Exercise 10: Shortest path

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D) shortest_paths(g, from = "A", to = "C")

  

Exercise 11: Connected components

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-B, C-D, E-F) components(g)

  

Exercise 12: Community detection (Louvain)

Difficulty: Advanced.

Show solution
RInteractive R
g <- erdos.renyi.game(20, 0.2) cluster_louvain(g)

  

Exercise 13: Edge list to data frame

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D) as_data_frame(g)

  

Exercise 14: Read from data frame

Difficulty: Intermediate.

Show solution
RInteractive R
edges <- data.frame(from = c("A","B","C"), to = c("B","C","D")) graph_from_data_frame(edges)

  

Exercise 15: Density

Difficulty: Intermediate.

Show solution
RInteractive R
g <- graph_from_literal(A-B, B-C, C-D, A-D, B-D) edge_density(g)

  

What to do next

  • Spatial-Analysis-Exercises (shipped), geographic networks.
  • Data-Visualization-Exercises (shipped), viz network plots.

Ready to earn the Network Analysis Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz