I have an igraph network with edge attributes.
I would like to generate a vertex attribute combining edge attributes. Specifically, I would like for each vertex to assign an attribute based on the mode of its own edge attributes (or any other operation, for the matter).
In my example an edge represent a collaboration between people on a subject.
library(igraph)
library(RColorBrewer)
g <- graph("Zachary") # the Zachary carate club
V(g)$names <- c(1:gorder(g))
set.seed(1); E(g)$relation <- sample(c("A","B","C"), gsize(g), replace = TRUE)
set.seed(1); E(g)$relation_col <- sample(brewer.pal(3, "Set1"), gsize(g), replace = TRUE)
plot(g, vertex.size=10, vertex.label=NA,
vertex.color="grey",
edge.color=E(g)$relation_col)
I cannot generate a vertex-specific attribute. When I calculate the mode, it is done for the whole network, not for the specific edge
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
g <- set_vertex_attr(g, "relation", value=getmode(E(g)$relation))
vertex_attr(g)
$names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
$relation
[1] "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
Manual reference: https://igraph.org/r/doc/igraph-attribute-combination.html