根据几列创建一个group_indices [英] Make a group_indices based on several columns

查看:51
本文介绍了根据几列创建一个group_indices的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成索引以基于两列对观察进行分组。但是,我希望小组由共同的观察组成,至少有一个共同的观察。我可以看到如何基于具有共同观察力但不只是其中之一的观察值进行分组。

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 can see how to make groups based on observations that share both observation in common, but not just one of them.

例如,使用数据框:

dt <- data.frame(id=1:10,
             G1 = c("A","A","B","B","C","C","C","D","E","F"),
             G2 = c("Z","X","X","Y","W","V","U","s","T","T"))

我想获取一列

1,1,1,1,2,2,2,3,4,4

我尝试过dplyr的group_indices,但是避风港

I tried with group_indices from dplyr, but haven't managed it.

推荐答案

使用 igraph 获取成员身份,然后映射名称:

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

这篇关于根据几列创建一个group_indices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆