How to do it...

  1. The first task is to load the required libraries:
library(statnet,quietly=TRUE)library(network,quietly=TRUE)  

The purpose of specifying the option quietly=TRUE is that a lot of comments are printed to the console and we can offset that with this option. Operationally, it makes no difference whether we use this option or not.

  1. Next, we specify a matrix with four rows and columns, a square matrix as expected, which will be then used to set up a network:
netmat1 <- rbind(c(0,1,1,0),                 c(1,0,1,0),                 c(1,1,0,1),                 c(0,0,1,0))rownames(netmat1) <- c("A","B","C","D")colnames(netmat1) <- c("A","B","C","D")netmat1  
  1. A consequence is the following output in the R console:
> netmat1A B C DA 0 1 1 0B 1 0 1 0C 1 1 0 1D 0 0 1 0
  1. The matrix ...

Get Practical Data Science Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.