首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R:识别图中的点(可以使用dplyr?)

R:识别图中的点(可以使用dplyr?)
EN

Stack Overflow用户
提问于 2020-11-26 08:07:18
回答 1查看 45关注 0票数 1

我在之前的stackoverflow帖子中找到了一个与我类似的问题,但答案并不完全相同:Check which community a node belongs in louvain community detection

我在R中创建了一些数据,然后制作了一个图。在制作完图之后,我对图进行了聚类。现在,假设我有一个人的列表,我想找出他们属于哪个集群。

我知道手动检查数据并找出这一点很容易,但我认为如果你有一个大数据集,这将是非常困难的。

我已经写了下面的代码。一切正常,直到最后两行,我试图找出"John","Peter“和"Tim”属于哪个集群:

代码语言:javascript
运行
复制
#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)

有人能告诉我我哪里做错了吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-26 08:16:45

折点的成员资格保存在$membership中,折点的名称保存在$names

代码语言:javascript
运行
复制
cluster$membership[match(people,cluster$names)]
#[1] 2 3 1

或者,如果您愿意,可以使用访问器函数igraph::membership

代码语言:javascript
运行
复制
membership(cluster)[people]
# John Peter   Tim 
#    2     3     1 

有关详细信息,请参阅help(communities)

示例数据:

代码语言:javascript
运行
复制
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")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65014400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档