简体   繁体   English

如何创建捕获 tidyverse 中多对多关系的组?

[英]How can I create groups that capture the many to many relationship in tidyverse?

I have a rather tricky question that I cannot seem to solve.我有一个相当棘手的问题,我似乎无法解决。 Consider the following table:考虑下表:

demo <- data.table(Person = c(1,2,2,3,4,5,6,4,7,8,9,10),
           Property = c("A","A","B","B","A","B","C","C","D","E","F","E"),
           Period = rep(2017, 12))

One person can own multiple properties and multiple properties can be owned by one person, hence a many to many relationship.一个人可以拥有多个财产,一个人可以拥有多个财产,因此是多对多的关系。 However, what I would like to do is create an one to one relationship from this many to many relationship by aggregation.但是,我想做的是通过聚合从这种多对多关系中创建一对一关系。 We can namely say: a bunch of people bought these bunch of properties.我们可以说:一堆人买了这些一堆房产。

In our demo case:在我们的demo案例中:

person 1 bought A person 2 bought A person 4 bought A person 2 bought B person 3 bought B person 5 bought B person 4 bought C person 6 bought C人1买A人2买A人4买A人2买B人3买B人5买B人4买C人6买C

The group of people (1,2,3,4,5,6) bought properties (A,B,C).这组人 (1,2,3,4,5,6) 购买了房产 (A,B,C)。 Let's call this group G1, so I would like to have a table that links the people 1 to 6 to group G1, and the properties A to C to G1.让我们称这个组为 G1,所以我想有一个表,将人 1 到 6 链接到组 G1,将属性 A 到 C 链接到 G1。

The following other groups can also be found: 7 and D, should be linked to group G2还可以找到以下其他组:7 和 D,应链接到组 G2

The group of people 8, 9 and 10 bought properties E and F. Thus, these should be linked to group G3. 8、9 和 10 组购买了房产 E 和 F。因此,这些应该与组 G3 相关联。

A result should give us two tables, namely: people_group and prop_Group.结果应该给我们两个表,即:people_group 和prop_Group。

people_group <- data.table(Person = c(1:10),
                           Group = c(rep("G1", 6), "G2", rep("G3", 3)))

prop_group <- data.table(Property = c("A", "B", "C", "D", "E", "F"),
                           Group = c(rep("G1", 3), "G2", rep("G3", 2)))

This should also be done per period, not just 2017 .这也应该在每个时期完成,而不仅仅是2017 Would this be possible in tidyverse / data.table?这在 tidyverse / data.table 中可能吗?

Here is a start using igraph :这是使用igraph的开始:

library(igraph)

# convert to graph object
g <- graph_from_data_frame(demo)
plot(g)

在此处输入图片说明

# get membership
x <- clusters(g)$membership

# add memberships
demo$grp <- x[ demo$Person ]

demo
#     Person Property Period grp
#  1:      1        A   2017   1
#  2:      2        A   2017   1
#  3:      2        B   2017   1
#  4:      3        B   2017   1
#  5:      4        A   2017   1
#  6:      5        B   2017   1
#  7:      6        C   2017   1
#  8:      4        C   2017   1
#  9:      7        D   2017   2
# 10:      8        E   2017   3
# 11:      9        F   2017   4
# 12:     10        E   2017   3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM