0

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.

lwe
  • 323
  • 1
  • 8
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 01 '19 at 16:33
  • @MrFlick, I've updated my post with some more clarity. Does that help? – lwe Nov 01 '19 at 17:02
  • How about `DF = data.frame(NAME=V(subnet)$name, EIGEN=V(subnet)$eigen)` ? – G5W Nov 01 '19 at 18:04
  • That did it! Thank you so much! @G5W! – lwe Nov 01 '19 at 18:15

0 Answers0