I calculated a centrality statistic on some network data in igraph, and I need help with two things (I think).
First, I want to move the vertex name (V(g)$name
) and the new centrality measure I calculated ((V(g)$eigen
) out of igraph an into a data frame.
This is the first line of the $names
and $eigen
in my graph.
$names: year young head black cent call
$eigen: 0.043284327 0.017877101 0.015949110 0.022489540 0.047533029 0.035666735
I've come up with the code below, but it is only pulling the vertex names, and not including the (V(g)$eigen
statistic I calculated.
Second, once I have both columns in the dataframe, I would like the dataframe sorted by the (V(g)$eigen
statistic, with the largest values displaying at the top.
#assigning eigenvector value to vertices
V(subnet)$eigen = eigen_centrality(subnet, weights = E(subnet)$weight)
#export two-column vector (vertex name, centrality)
subnet_matrix <- as.matrix(c(V(subnet)$name), V(subnet)$eigen)
#create new data frame with just those two columns
subnet_df = as.data.frame(subnet_matrix)
#sort df by the centrality value (biggest at top)
sort.list(subnet_df$eigen)
What I want it to end up looking like is this (in a data frame):
NAME EIGEN
cent 0.047533029
year 0.043284327
call 0.035666735
black 0.022489540
young 0.017877101
head 0.015949110
Any help would be greatly appreciated. I'm new to R and coding in general, so I've been stuck on this way too long.