2014-01-19 73 views
1

我有大量的鄰接矩陣,以csv格式從excel中導出。我也有大量的csv。具有頂點屬性數據的文件。 我已經在SNA中鏈接了它們,但是igraph在功能上進一步發展,所以我期望移動到它,但是我無法構建圖+屬性文件。節點屬性csv igraph

我正在尋找設置一些代碼,這將是做一系列情節的主力。 雖然似乎有很多方法這兩個數據鏈路集,似乎這是最簡單的: 要在CSV鄰接矩陣的數據幀(減少丟失的頂點數據)我使用:

m <- read.table(header=TRUE, check.names=FALSE, textConnection(" 
     2 3 4 5 6 7 
    2 0 1 1 0 1 0 
    3 1 0 0 0 1 0 
    4 0 0 0 0 0 0 
    5 1 0 1 0 0 1 
    6 0 0 0 0 0 0 
    7 1 1 0 1 0 0 
")) 

在在原始文件中同時具有頂點和行名稱的情況下,導入的屬性文件具有與節點名稱對應的頂點名稱和「row.names」。 Hex.ed[1,1]給出了米網絡中的第一節點的屬性的值,即節點2:

Hex.ed <- read.table(header=TRUE, textConnection(" 
     HH Emo Extra Aggr Consci OTE 
    2 3.3750 3.0000 3.0000 3.0000 3.0625 3.4375 
    3 3.5625 2.9375 3.0625 3.0000 3.3125 3.6250 
    4 3.2500 2.8750 3.7500 3.2500 3.8750 3.5000 
    5 3.6875 3.1250 3.3750 3.5625 3.6250 3.3125 
    6 3.3125 3.0000 3.3125 3.8750 3.2500 3.6875 
    7 3.8125 3.2500 3.5625 2.8750 3.6875 3.4375 
")) 

g <- graph.data.frame(m, directed=TRUE, vertices=Hex.ed) 

然而,我得到的錯誤:Error in graph.data.frame(m, directed = TRUE, vertices = Hex.ed) : Duplicate vertex names

回答

0

我得到一個不同的錯誤消息:

Error in graph.data.frame(m, directed = TRUE, vertices = Hex.ed) : 
    Some vertex names in edge list are not listed in vertex data frame 

但這是因爲您沒有在問題中運行示例,而是可能使用了完整的數據集。

無論如何,graph.data.frame不使用鄰接矩陣。從文檔在http://igraph.sourceforge.net/doc/R/graph.data.frame.html

... the first two columns of d are used as a symbolic edge list and additional columns as edge attributes. The names of the attributes are taken from the names of the columns.

如果你關心閱讀你會看到在底部一個例子手冊。

如果你有一個鄰接矩陣,那麼你可以使用graph.adjacency創建圖形,然後逐個增加頂點屬性之一:

g <- graph.adjacency(as.matrix(m)) 
for (i in seq_len(ncol(Hex.ed))) { 
    g <- set.vertex.attribute(g, colnames(Hex.ed)[i], value=Hex.ed[,i]) 
} 
g 
# IGRAPH DN-- 6 11 -- 
# + attr: name (v/c), HH (v/n), Emo (v/n), Extra (v/n), Aggr (v/n), 
# Consci (v/n), OTE (v/n) 
+0

許多感謝的Gabor。我會高興地分享csv文件,這將使其重現,但我不知道如何做到這一點。 – user14470

+0

嗨,我努力使其在網站上可重現,並且我還不夠高級發佈圖像,但我希望數據的格式清晰。 – user14470

+0

我不確定這將是多麼可重複,但希望你會看到足以發現我的錯誤。謝謝你的耐心。 – user14470