2016-12-15 159 views
0

我有一個有向圖並希望導出一個頂點表,其中包含「度」,「出度」和「總度」等度量在一個。R igraph degree()'error in match.arg'

g <- graph(c("John", "Jim", "Jim", "Jill", "Jill", "John")) 

既然我們有一個樣本有向圖,我想要列出每個頂點的入,出和總度。

degree(g, mode = c("in", "out", "total")) 

返回此錯誤:

錯誤match.arg(ARG ARG =,選擇=選擇,several.ok = several.ok): 'ARG' 必須是長度爲1的

我在做什麼錯?我可以單獨做每一個,但我不會如何將它們連接在一起。

回答

1

igraph中的degree函數不接受那樣的多個參數。使用sapply在不同的調用迭代mode說法:

sapply(list("in","out","total"), function(x) degree(g, mode = x)) 

它返回的連續列中的值:

> sapply(list("in","out","total"), function(x) degree(g, mode = x)) 
    [,1] [,2] [,3] 
John 1 1 2 
Jim  1 1 2 
Jill 1 1 2 
0

使每個個體,出後,和總榜單,

idl <- degree(g, mode="in") 
odl <- degree(g, mode="out") 
tdl <- degree(g, mode="total") 

將它們轉換爲數據幀

idl <- data.frame(idl) 
odl <- data.frame(odl) 
tdl <- data.frame(tdl) 

然後結合使用cbind

> cbind(idl,odl,tdl) 
    idl odl tdl 
John 1 1 2 
Jim 1 1 2 
Jill 1 1 2