R igraph度数() 'Error in match.arg'

R igraph degree() 'Error in match.arg'

我有一个有向图,我想导出 table 个具有 "in degree"、"out degree" 和 "total degree" 等指标的顶点。

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

现在我们有了一个样本有向图,我想得到每个顶点的入度、出度和总度数。

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

返回此错误:

Error in match.arg(arg = arg, choices = choices, several.ok = several.ok) : 'arg' must be of length 1

我做错了什么?我可以单独做每一个,但我不知道如何将它们连接在一起。

igraph 中的 degree 函数不接受这样的多个参数。使用 sapply 遍历 mode 参数的不同调用:

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

它 returns 连续列中的值:

> 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

在列出每个人的进出和总名单后,

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