I am trying to add centrality measures as attributes to a "master graph", g_master. Here is my code:
library(igraph)
#generate master graph
g <- sample_pa(10000)
g_in <- degree(g, mode="in")
g_out <- degree(g, mode="out")
g_inclo <- closeness(g, mode="in")
g_outclo <- closeness(g, mode="out")
g_bet <- betweenness(g)
set_vertex_attr(g, "name", index=V(g), value = V(g))
g_master <- data.frame(V(g), g_in, g_out, g_inclo, g_outclo, g_bet)
But I get the following:
> g_master <- data.frame(V(g), g_in, g_out, g_inclo, g_outclo, g_bet)
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""igraph.vs"" to a data.frame
Other parts of the codes are fine.
As the error says, it faces problems trying to use something of class igraph.vs
. In particular, it is V(g)
what causes problems. But we can coerce it as follows:
g_master <- data.frame(V = as.vector(V(g)), g_in, g_out, g_inclo, g_outclo, g_bet)
head(g_master, 2)
# V g_in g_out g_inclo g_outclo g_bet
# 1 1 208 0 2.193608e-05 1.0001e-08 0
# 2 2 48 1 1.042957e-08 1.0002e-08 411