记录最连接的节点 [英] r igraph most connected nodes
问题描述
我有一张图:
paths = data.frame(from=c(10,20,30,10,30), to=c(20,30,50,30,60))
g <- graph_from_data_frame(paths, directed=FALSE)
plot(g)
是否有命令找出每个节点的连接数,并找到连接最多的节点.在此示例中,30个节点是连接最多的节点,具有4个连接.然后是20和10,每个都有2个连接.
Is there a command to find out the number of connections of each node, and find the most connected node. In this example 30 is the most connected node, with 4 connections. Then come 20 and 10 with 2 connections each.
推荐答案
在这种情况下,由于出现了两个向量中的一个节点表示1个连接):
In this case you can just calculate it from the data.frame, by counting number of connections (treating from & to as the same, since the appearance of a node in either vector means 1 connection):
sort(table(c(paths$from, paths$to)), decreasing = TRUE)
结果:
30 10 20 50 60
4 2 2 1 1
说明:代码正在创建所有连接的向量(c(paths$from, paths$to)
),然后计算频率(table
),然后sort
计算结果以获得从连接最多到连接最少的有序列表(
Explanation: The code is creating a vector of all connections (c(paths$from, paths$to)
), then counting frequency (table
), then sort
ing the result to get an ordered list, from most to least connected (decreasing=TRUE
).
这篇关于记录最连接的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!