首页 > 解决方案 > 将顶点属性数据从 igraph 移动到矩阵

问题描述

我计算了 igraph 中一些网络数据的中心性统计数据,我需要两件事的帮助(我认为)。

首先,我想将顶点名称 ( V(g)$name) 和我计算的新中心度度量 ( (V(g)$eigen) 从 igraph 中移出到数据框中。

这是我图表中$namesand的第一行。$eigen

$names: year       young        head       black        cent        call      
$eigen: 0.043284327 0.017877101 0.015949110 0.022489540 0.047533029 0.035666735 

我想出了下面的代码,但它只是提取顶点名称,不包括(V(g)$eigen我计算的统计数据。

其次,一旦我在数据框中拥有两列,我希望数据框按(V(g)$eigen统计数据排序,最大值显示在顶部。

    #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)

我希望它最终看起来像这样(在数据框中):

NAME     EIGEN
cent     0.047533029
year     0.043284327
call     0.035666735
black    0.022489540
young    0.017877101
head     0.015949110

任何帮助将不胜感激。我是 R 和一般编码的新手,所以我一直坚持这种方式太久了。

标签: rdataframeigraphdata-transform

解决方案


推荐阅读