Reputation: 364
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.
Upvotes: 0
Views: 774
Reputation: 364
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
Upvotes: 0
Reputation: 3729
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
Upvotes: 1