2012-06-08 29 views
15

我的一般問題是,當使用iGraph生成圖時,我丟失了頂點名稱/標籤(不確定這裏是否有正確的單詞)。iGraph圖中的頂點名稱在哪裏

我有二分網絡的邊緣列表IC_edge_sub,看起來像以下:

new_individualID new_companyID 
1    <NA>  10024354c 
3  10069415i  2020225c 
4  10069415i  16020347c 
5  10069272i  2020225c 
6  10069272i  16020347c 
7  10069274i  2020225c 

我然後創建圖形元素:

IC_projected_graphs <- bipartite.projection(IC_twomode, types = 
         is.bipartite(IC_twomode)$type) 

然後摺疊它識別之間只連接公司ID

IC_projected_graphs <- bipartite.projection(IC_twomode, types = 
         is.bipartite(IC_twomode)$type) 

然後得到鄰接矩陣:

CC_matrix_IC_based <- get.adjacency(CC_graph_IC_based); CC_matrix_IC_based 

在iGraph節點編號從零開始,因此矩陣命名也從零開始。然而,我現在需要在最終的基於CC_matrix_IC_b的矩陣中邊緣列表的第二列中指定的「new_companyID」。

你能幫助我如何使用原始邊界列表中的信息將rownames和colnames放入最終的鄰接矩陣中嗎?

我搜索了它並搜索了堆棧流,但無法真正找到可行的答案。非常感謝您的幫助

+1

感謝Andrie的編輯。我沒有看到我寫作創建的問題。真的很感激它。 – chiron1979

回答

22

頂點名稱通常存儲在igraph中名爲name的頂點屬性中。因此,如果圖表存儲在變量g中,則可以使用V(g)$name來檢索所有頂點的名稱。

+1

不幸的是,這在我的情況下不起作用。我只是回NULL。太糟糕了。任何其他想法都是偶然的。對不起,但我真的在這個問題上隱藏起來。 – chiron1979

+0

如何獲得數字頂點ID和名稱的關係? – pengchy

1

我知道,回答自己的問題相當放肆。

我想我已經解決了它。關鍵問題是我在生成圖表時沒有保存名稱。感謝Tamas。沒有她的回答,我不會意識到這一點。之後,我需要確保不會丟失數據。在下面的整體解決方案:

# Subsetting/triangulating data for selected games 
     GC_edge_sub <- subset (GC_edge, mb_titleID %in% loggames_yearly_sample$mb_titleID) 
     GC_edge_sub <- subset(GC_edge_sub, select=c("new_titleID", "new_companyID")) 
     head(GC_edge_sub) 

    # Generating the vertex names 
     vertex_new_companyID <- data.frame(names = unique(GC_edge_sub$new_companyID)) 
     vertex_new_titleID <- data.frame(names = unique(GC_edge_sub$new_titleID)) 
     vertex <- rbind(vertex_new_companyID,vertex_new_titleID) 

    # Creation of GC_twomode 
    GC_twomode <- graph.data.frame(GC_edge_sub, vertices = vertex) 
    GC_projected_graphs <- bipartite.projection(GC_twomode, types = is.bipartite(GC_twomode)$type) 
    GC_matrix_GC_based <- get.adjacency(GC_twomode) 
    dim(GC_matrix_GC_based) 

    # Collapsing the matrix 
     # Be aware that if you use the classical command # CC_graph_GC_based <- GC_projected_graphs$proj2 it collapses, but looses the colnames and rownames 
     # I thus a) create a subset of the adjacency matrix and b) create the lookef for matrix by multiplication  
     rowtokeep <- match(vertex_new_companyID$names,colnames(GC_matrix_GC_based)) 
     coltokeep <- match(vertex_new_titleID$names,rownames(GC_matrix_GC_based)) 
     GC_matrix_GC_based_redux <- GC_matrix_GC_based[rowtokeep,coltokeep] 
    # We now have a CG matrix.Let's build from this a GG matrix. 
     CC <- GC_matrix_GC_based_redux %*% t(GC_matrix_GC_based_redux)