karel lahmy
karel lahmy

Reputation: 417

Find name of node associated with maximum degree in igraph data frame

I am using the igraph package to find degree of each node (the built in degree(g) function) and it returns a numeric vector. How can I tell which node has the maximum degree (not the value but the node name)?

Upvotes: 3

Views: 7948

Answers (2)

Tchotchke
Tchotchke

Reputation: 3121

If you have a igraph data frame G, then you can create a TRUE/FALSE vector with degree(G)==max(degree(G)). You can then use that to find the name of the node(s) that meet that criteria - V(G)$name[degree(G)==max(degree(G))].

I created a small example to illustrate:

library(igraph)
df = data.frame(node1=c("Bob", "Jim", "Dave", "Dave"),
                node2=c("Jane", "John", "Sally", "Al"))

G = graph.data.frame(df)
V(G)$name[degree(G)==max(degree(G))]
[1] "Dave"

Upvotes: 10

CCurtis
CCurtis

Reputation: 1932

Example data

dat<-sample(0:100,100,rep=FALSE)
maximum<-dat[order(dat,decreasing = TRUE)

Verify

maximum

Upvotes: 2

Related Questions