Below are the solutions to these exercises on analysing networks.

#################### # # # Exercise 1 # # # #################### d <- read.csv("http://r-exercises.com/wp-content/uploads/2016/10/sociogram-employees-un.csv", header=FALSE) g <- graph.adjacency(as.matrix(d), mode="undirected") V(g)$name <- LETTERS[1:NCOL(d)] V(g)$color <- "orange" V(g)$shape <- "square" E(g)$color <- "blue" E(g)$arrow.size <- 0.2 plot(g)

#################### # # # Exercise 2 # # # #################### largest_cliques(g)

## [[1]] ## + 10/25 vertices, named: ## [1] D A K V I Q B H J X

#################### # # # Exercise 3 # # # #################### maximal.cliques.count(g)

## [1] 128

#################### # # # Exercise 4 # # # #################### cohesion(g)

## [1] 13

#################### # # # Exercise 5 # # # #################### cluster_edge_betweenness(g)

## IGRAPH clustering edge betweenness, groups: 5, mod: 0.0016 ## + groups: ## $`1` ## [1] "A" "B" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" ## [18] "V" "W" "X" "Y" ## ## $`2` ## [1] "C" ## ## $`3` ## [1] "S" ## ## + ... omitted several groups/vertices

#################### # # # Exercise 6 # # # #################### components(g)

## $membership ## A B C D E F G H I J K L M N O P Q R S T U V W X Y ## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ## ## $csize ## [1] 25 ## ## $no ## [1] 1

#################### # # # Exercise 7 # # # #################### E(g)[which_loop(g)==TRUE]

## + 0/210 edges (vertex names):

#################### # # # Exercise 8 # # # #################### sum(count_triangles(g))

## [1] 2421

count_triangles(g, V(g)['S'])

## [1] 63

#################### # # # Exercise 9 # # # #################### transitivity(g, type="global")

## [1] 0.7145809

transitivity(sample_gnm(vcount(g), ecount(g)), type="global")

## [1] 0.7051546

# No, the clustering is not significant, since clastering coefficient of network # is just little greater than clustering coefficient of corresponding # random network #################### # # # Exercise 10 # # # #################### plot(sample_gnp(10, 0.4))

plot(make_full_graph(15))

plot(graph.star(20))

g.ring <- make_ring(15, directed=TRUE, mutual=TRUE) E(g.ring)$arrow.size <- 0.2 plot(g.ring)

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply