how to count how many times vertices are linked to vertices with a different value on a specific attribute

StackOverflow https://stackoverflow.com/questions/22913694

  •  29-06-2023
  •  | 
  •  

I have a graph like:

library(igraph)
g <- erdos.renyi.game(10, 0.5)
V(g)$rel <- rbinom(10,1,0.5)

I would like to count, for vertices with $rel == 0, how many times they connect with a vertex with $rel == 1 and for vertices with $rel == 1 how many times they connect with vertices with $rel == 0.

It is like an assortativity for a specific attribute, but I'd like to have the result separated for each of the two groups, and not an overall value for the whole network.

有帮助吗?

解决方案

Remove the edges that connect rel==0 and rel==1 vertices, and then just calculate the degree of the vertices.

el <- get.edgelist(g)
el[]  <- V(g)$rel[el]
g2 <- delete.edges(g, which(el[,1] == el[,2]))
degree(g2)
# [1] 3 2 4 1 3 2 2 4 4 3

If the attribute is not numeric (EDIT)

el <- get.edgelist(g)
mode(el) <- "character"
el[]  <- V(g)$rel[el]
g2 <- delete.edges(g, which(el[,1] == el[,2]))
degree(g2)
# [1] 3 2 4 1 3 2 2 4 4 3
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top