Get Started

Counting vertices and edges

A lot of basic information about a network can be extracted from an igraph object. In this exercise you will learn how to count the vertices and edges from a network by applying several functions to the graph object g.

Each row of the friends dataframe represents an edge in the network.

This is a part of the course

“Network Analysis in R”

View Course

Exercise instructions

  • Use V() and E() to view the vertices and edges respectively of the network.
  • Use gsize() to count the number of edges in a network.
  • Use gorder() to count the number of vertices in a network.

Hands-on interactive exercise

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

# Load igraph
library(igraph)

# Subset vertices and edges
V(___)
E(___)

# Count number of edges
gsize(___)

# Count number of vertices
____(g)

This exercise is part of the course

Network Analysis in R

IntermediateSkill Level
4.8+
6 reviews

Learn to analyze and visualize network data with the igraph package and create interactive network plots with threejs.

In this chapter, you will be introduced to fundamental concepts in social network analysis. You will learn how to use the <code>igraph</code> R package to explore and analyze social network data as well as learning how to visualize networks.

Exercise 1: What are social networks?Exercise 2: Creating an igraph objectExercise 3: Counting vertices and edges
Exercise 4: Network attributesExercise 5: Node attributes and subsettingExercise 6: Edge attributes and subsettingExercise 7: Visualizing attributesExercise 8: Quiz on attributesExercise 9: Network visualizationExercise 10: igraph network layoutsExercise 11: Visualizing edgesExercise 12: Quiz on igraph objects

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free