r-互连案例的唯一ID



我有以下数据帧,它显示了哪些情况是相互关联的:

DebtorId DupDebtorId
1:        1           2
2:        1           3
3:        1           4
4:        5           1
5:        5           2
6:        5           3
7:        6           7
8:        7           6

我的目标是为每组案例分配一个唯一的组ID。所需输出为:

DebtorId group
1:        1     1
2:        2     1
3:        3     1
4:        4     1
5:        5     1
6:        6     2
7:        7     2

我的思路:

library(data.table)
example <- data.table(
DebtorId = c(1,1,1,5,5,5,6,7),
DupDebtorId = c(2,3,4,1,2,3,7,6)
)
unique_pairs <- example[!duplicated(t(apply(example, 1, sort))),] #get unique pairs of DebtorID and DupDebtorID
unique_pairs[, group := .GRP, by=.(DebtorId)] #assign a group ID for each DebtorId
unique_pairs[, num := rowid(group)]
groups <- dcast(unique_pairs, group + DebtorId ~ num, value.var = 'DupDebtorId') #format data to wide for each group ID
#create new data table with unique cases to assign group ID
newdt <- data.table(DebtorId = sort(unique(c(example$DebtorId, example$DupDebtorId))), group = NA)
newdt$group <- as.numeric(newdt$group)
#loop through the mapped groups, selecting the first instance of group ID for the case
for (i in 1:nrow(newdt)) {
a <- newdt[i]$DebtorId
b <- min(which(groups[,-1] == a, arr.ind=TRUE)[,1])
newdt[i]$group <- b
}

输出:

DebtorId group
1:        1     1
2:        2     1
3:        3     1
4:        4     1
5:        5     2
6:        6     3
7:        7     3

我的方法有两个问题:

  1. 从输出中,您可以看到它无法识别情况5属于第1组
  2. 最后一个循环非常缓慢对于我的原始数据中1M行的用例来说,这将是无用的,并且采用传统的:=方式对which()不起作用

我不确定我的方法是否可以优化,或者有更好的方法来实现这一点。

igraph中已经存在此功能,因此如果您不需要自己动手,我们可以从您的数据框架中构建一个图,然后提取集群成员身份。stack()只是将命名向量转换为数据帧的一种简单方法。

library(igraph)
g <- graph.data.frame(df)
df_membership <- clusters(g)$membership
stack(df_membership)
#>   values ind
#> 1      1   1
#> 2      1   5
#> 3      2   6
#> 4      2   7
#> 5      1   2
#> 6      1   3
#> 7      1   4

以上,values对应于groupind对应于DebtorId

最新更新