Make a group_indices based on several columns

前端 未结 1 583
陌清茗
陌清茗 2020-11-27 07:54

I would like to generate indices to group observations based on two columns. But I want groups to be made of observation that share, at least one observation in commons. I c

相关标签:
1条回答
  • 2020-11-27 08:10

    Using igraph get membership, then map on names:

    library(igraph)
    
    # convert to graph, and get clusters membership ids
    g <- graph_from_data_frame(df1[, c(2, 3, 1)])
    myGroups <- components(g)$membership
    
    myGroups 
    # A B C D E F Z X Y W V U s T 
    # 1 1 2 3 4 4 1 1 1 2 2 2 3 4 
    
    # then map on names
    df1$group <- myGroups[df1$G1]
    
    
    df1
    #    id G1 G2 group
    # 1   1  A  Z     1
    # 2   2  A  X     1
    # 3   3  B  X     1
    # 4   4  B  Y     1
    # 5   5  C  W     2
    # 6   6  C  V     2
    # 7   7  C  U     2
    # 8   8  D  s     3
    # 9   9  E  T     4
    # 10 10  F  T     4
    
    0 讨论(0)
提交回复
热议问题