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