简体   繁体   中英

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

I have a directed graph and would like to export a table of vertices with metrics such as "in degree", "out degree", and "total degree", all in one.

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

Now that we have a sample directed graph, I would like to get the in, out, and total degree listed for each vertices.

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

This error is returned:

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

What am I doing wrong? I could do each one individually but I don't how to concatenate them all together.

The degree function in igraph does not accept multiple arguments like that. Use sapply to iterate over the different calls the mode argument:

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

It returns the values in consecutive columns:

> 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

After making each individual in, out, and total list,

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

convert them to a data frame

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

then combine using cbind

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM