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
  •  | 
  •  

Domanda

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.

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top