【问题标题】:R: identifying points in a graph (possible with dplyr?)R:识别图中的点(可能使用 dplyr?)
【发布时间】:2020-11-26 00:07:18
【问题描述】:

我发现了一篇以前的 stackoverflow 帖子,它处理了我遇到的类似问题,但那里的答案并不完全相同:Check which community a node belongs in louvain community detection

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

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

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

#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)
        

谁能告诉我我做错了什么?

谢谢

【问题讨论】:

  • 我想你的意思是graph &lt;- graph.data.frame(Data_I_Have, directed=F)plot(cluster, graph)

标签: r graph dplyr data-visualization nodes


【解决方案1】:

顶点的成员关系保存在$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")

【讨论】:

  • 不错。我已经多次使用这些功能,但总是对哪些帮助文件与 igraph 中的内容相关感到困惑。这真的很有帮助。
  • @Ian:谢谢!这正是我想要的! (我修正了错字,谢谢!)
  • 以下是如何将其制作成表格:a = members(cluster)[people] b = data.frame(a) b$names
  • 排序:s = b[order(b$a, reduction = TRUE),]
猜你喜欢
  • 2018-07-04
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 2021-07-10
  • 2019-12-07
  • 1970-01-01
相关资源
最近更新 更多