简体   繁体   中英

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:

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

The group of people (1,2,3,4,5,6) bought properties (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.

The following other groups can also be found: 7 and D, should be linked to group G2

The group of people 8, 9 and 10 bought properties E and F. Thus, these should be linked to group G3.

A result should give us two tables, namely: people_group and 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 . Would this be possible in tidyverse / data.table?

Here is a start using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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