user3485076
user3485076

Reputation: 33

How can I count the second degree of all vertices in a network graph in R?

The number of first degree connections in a network (random graph) is easily counted with with the function:

g <- erdos.renyi.game(10, 1/10, directed = TRUE)
d1 <- degree(g, mode="out")

However, with which function can I count the second degree connections of each node?

Upvotes: 3

Views: 681

Answers (1)

shadow
shadow

Reputation: 22343

I suggest using neigborhood as an alternative to the degree function. This allows you to specify any order of the neighborhood.

d1 <- sapply(neighborhood(g, 1, mode="out"), length)-1
d2 <- sapply(neighborhood(g, 2, mode="out"), length)-1

If you have graphs with loops, you should check if this still gives the desired result.

Edit: Thanks @Ryan for pointing out that neighborhood.size is actually the much better answer than neighborhood.

d1 <- neighborhood.size(g, 1, mode="out")-1
d2 <- neighborhood.size(g, 2, mode="out")-1

Edit: Thanks @Ryan for the neighborhood.size function! I used it and it works, however in order to optain the number of the second degree connections you need to subtract d1 from d2, then you only have the second degree connections:

d1 <- degree(g, mode="out")
d2 <- neighborhood.size(g, 2, mode="out")-d1-1

Upvotes: 3

Related Questions