I am working with an undirected igraph object composed of nodes of different types (e.g. males M in yellow and females F in orange):
g <- graph.atlas(711)
V(g)$name <- 1:7
V(g)$gender <- c("M","F","M","M","M","F","F")
V(g)$color <- ifelse(V(g)$gender=="F", "orange","yellow")
g<-delete.edges(g, E(g, P=c(1,2,2,3,2,7,7,6,7,3,3,4,3,5,4,5,5,6,6,1)))
g<-add.edges(g,c(1,4,4,5,5,1,4,7,7,3,3,5,5,7,2,7,7,6,6,2,6,4))
plot(g)
I would like to extract an edge list composed of the edges connecting nodes of different types (males and females):
edgelist <- rbind(c(3,7),
c(4,6),
c(4,7),
c(5,7))
assortativity
uses the fraction of edges connecting vertices of type M and F, but I do not know to extract explicity these edges.
get.edgelist
only returns the whole edge list with no possibility of setting conditions.
You can use the %--%
selector to find edges that connect male nodes to female nodes. For example
E(g)[V(g)[gender=="M"] %--% V(g)[gender=="F"]]
The V(g)[gender=="M"]
finds all the "male" nodes and V(g)[gender=="F"]
finds all the female node and %--%
finds all the edges between the two sets.
edges = get.edgelist(g)
edges[rowSums(apply(edges, 2, function(x) get.vertex.attribute(g, "gender", x)) == "M") == 1,]
# [,1] [,2]
#[1,] 4 7
#[2,] 3 7
#[3,] 5 7
#[4,] 4 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With