r - R:识别图中的点(可能使用 dplyr?)
问题描述
我发现以前的 stackoverflow 帖子处理了我遇到的类似问题,但答案并不完全相同:Check which community a node belongs in louvain community detection
我在 R 中创建了一些数据,然后制作了一个图表。制作完图后,我在图上进行了聚类。现在,假设我有一个人员列表,我想找出他们属于哪个集群。
我知道手动检查数据并找出这一点很容易,但是我认为如果您有一个大数据集,这将非常困难。
我已经写了下面的代码。一切正常,直到最后两行我试图找出“约翰”、“彼得”和“蒂姆”属于哪些集群:
#load libraries
library(igraph)
library(dplyr)
#create data
Data_I_Have <- data.frame(
"Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
"Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude")
)
#create graph
graph <- graph.data.frame( Data_I_Have, directed=F)
graph <- simplify(graph)
#perform clustering
cluster = cluster_louvain(graph)
#plot graph
plot(graph, cluster)
#make list of people
people <- c("John", "Peter", "Tim")
#find out which cluster each of these people belong in (here is the error)
location <- names("people")[!(names("people") %in% cluster)]
#transform the previous data frame into a table
location_table <- table(location)
有人可以告诉我我做错了什么吗?
谢谢
解决方案
顶点的成员资格保存在$membership
,顶点的名称在$names
:
cluster$membership[match(people,cluster$names)]
#[1] 2 3 1
或者,如果您愿意,可以使用访问器功能igraph::membership
:
membership(cluster)[people]
# John Peter Tim
# 2 3 1
有关help(communities)
更多信息,请参阅。
示例数据:
cluster <- structure(list(membership = c(2, 3, 1, 1, 1, 2, 2, 3), memberships = structure(c(2,
3, 1, 1, 1, 2, 2, 3), .Dim = c(1L, 8L)), modularity = 0.115702479338843,
names = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier",
"Claude", "Henry"), vcount = 8L, algorithm = "multi level"), class = "communities")
推荐阅读
- php - 添加 Nonce 和 CSP 的 WordPress/Woocommerce 内联 Javascript 未按预期工作
- node.js - 使用令牌创建身份验证 Web 服务
- r - 无法在 Windows 上安装处理器和过程
- database - 我应该如何将带有格式的 QTextEdit 内容保存到数据库?toPlainText 方法无法获取格式
- java - 从 Spring Boot 中的 org.joda.time.Interval 迁移
- python - 如果 MySQL 数据库连接池在很长一段时间内未使用或使用它的应用程序崩溃,它是否会过期?
- python - 配置 Pycharm/odoo 12
- datatable - 数据表 YADCF 服务器端过滤器类型
- ios - React-Native iOS - 如何在启动屏幕之后显示视图控制器,然后再初始化反应原生桥(iOS)
- python - Edabit 任务未显示正确结果