Selrac
Selrac

Reputation: 2293

Count the nodes with most connection in a network in R igraph

I want to count the number nodes with more received connections in a unidirectional network.

For example in a network like this:

g <- graph( c('A',1,'A',2,'A',3,'B',4,'B',5,'B',6,'C',7,'C',8,'D',7,'D',8))

We would have 3 independent components

Loading the data in a file (test), I can upload it and represent it as follows:

plot(g)

plot graph

How can I get the destination nodes with more connections? In this case will be node 7 and 8.

Following another question (r igraph most connected nodes) I tried the following:

lengths(as_adj_list(g))

A 1 2 3 B 4 5 6 C 7 8 D 
3 1 1 1 3 1 1 1 2 2 2 2 

The result is counting the lengths of all the nodes, but I'm looking at the destination nodes only.

I tried also:

 sort(g, decreasing = TRUE)

 Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : index larger than maximal 12

As you see I get an error message

Following on commentary: With the following I get the count of the destination nodes, but how do I get the ones with the maximum count?

degree(g4, mode = "in")

Any ideas?

Thanks

Upvotes: 2

Views: 2324

Answers (1)

lukeA
lukeA

Reputation: 54237

You could do

library(igraph)
g <- graph( c('A',1,'A',2,'A',3,'B',4,'B',5,'B',6,'C',7,'C',8,'D',7,'D',8))
V(g)$indeg <- degree(g, mode = "in")
V(g)[V(g)$indeg == max(V(g)$indeg)]
#  2/12 vertices, named:
# [1] 7 8

Upvotes: 5

Related Questions