扩展列表以在组中包括所有可能的成对组合 [英] Expanding a list to include all possible pairwise combinations within a group
问题描述
我目前正在进行随机化处理,其中将给定人群的个体采样并放入定义大小的组中.结果是一个数据帧,如下所示:
I am currently running a randomization where individuals of a given population are sampled and placed into groups of defined size. The result is a data frame seen below:
Ind Group
Sally 1
Bob 1
Sue 1
Joe 2
Jeff 2
Jess 2
Mary 2
Jim 3
James 3
有没有能让我扩展数据集以显示组配对中所有可能的功能?(下面的所需输出).配对不需要是互惠的.
Is there a function which will allow me to expand the data set to show every possible within group pairing? (Desired output below). The pairings do not need to be reciprocal.
Group Ind1 Ind2
1 Sally Bob
1 Sally Sue
1 Sue Bob
2 Joe Jeff
2 Joe Jess
2 Joe Mary
2 Jeff Jess
2 Jess Mary
2 Jeff Mary
3 Jim James
我觉得必须在dplyr中做到这一点,但对于我的一生,我似乎无法解决它.
I feel like there must be a way to do this in dplyr, but for the life of me I can't seem to sort it out.
推荐答案
以下是使用 data.table
的选项.转换为 data.table
( setDT(dt)
),进行按"Group"分组的交叉联接( CJ
),并删除重复的
元素
Here is an option using data.table
. Convert to data.table
(setDT(dt)
), Do a cross join (CJ
) grouped by 'Group' and remove the duplicated
elements
library(data.table)
setDT(dt)[, CJ(Ind1 = Ind, Ind2 = Ind, unique = TRUE)[Ind1 != Ind2],
Group][!duplicated(data.table(pmax(Ind1, Ind2), pmin(Ind1, Ind2)))]
# Group Ind1 Ind2
#1: 1 Bob Sally
#2: 1 Bob Sue
#3: 1 Sally Sue
#4: 2 Jeff Jess
#5: 2 Jeff Joe
#6: 2 Jeff Mary
#7: 2 Jess Joe
#8: 2 Jess Mary
#9: 2 Joe Mary
#10: 3 James Jim
或按组"使用 combn
setDT(dt)[, {temp <- combn(Ind, 2); .(Ind1 = temp[1,], Ind2 = temp[2,])}, Group]
这篇关于扩展列表以在组中包括所有可能的成对组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!