从组成员数据创建对

Creating pairs from Group Membership Data

首先,对于这个可能很愚蠢的问题,我深表歉意,但在使用 google 并执行跟踪和错误 7 小时后,我感到非常沮丧和绝望...

我有一个用户 ID 和他们所属的组的列表。我需要共享一个组的所有用户组合的列表(用于创建网络图的边缘列表)。我马上找到 this 并且非常高兴,因为这正是我所需要的。我以前从未使用过 R,但它似乎可以很容易地解决我的问题。另一个线程中提供的代码按原样运行得非常好,但是在我开始根据我的需要(尤其是我的数据输入)对其进行自定义后,我 运行 遇到了问题:

#import a csv, the column "group" consists of groupID, column "user" the userID
group <- read.csv("E:/r/input.csv")[ ,c('group')]
user <- read.csv("E:/r/input.csv")[ ,c('user')]
data.frame(group,user)

R 中的输出给我这个:

       group       user
1  596230112 1514748421
2  596230112 1529087841
3  596230112 1518194516
4  596230112 1514852264
5  596230112 1514748421
6  596230112 1511768387
7  596230112 1514748421
8  596230112 1514852264
9  596230112 1511768387
10 596231111 1535990615
11 596232665 1536087573
12 596232665 1488758238
13 596232665 1536087573
14 596234505 1511768387
15 596234505 1535990615

到目前为止,一切顺利!下一步应该将用户配对,例如

1512748421 -> 1529097841
1512748421 -> 1518194516 

等等...我使用的代码是:

#create pairs
pairs <- do.call(rbind, sapply(split(user, group), function(x) t(combn(x,2))))

我得到的错误是:

Error : cannot allocate vector of size 5.7 Gb
In addition: Warning messages:
1: In combn(x, 2) :
  Reached total allocation of 3981Mb: see help(memory.size)
2: In combn(x, 2) :
  Reached total allocation of 3981Mb: see help(memory.size)
3: In combn(x, 2) :
  Reached total allocation of 3981Mb: see help(memory.size)
4: In combn(x, 2) :
  Reached total allocation of 3981Mb: see help(memory.size)

我最终想要使用的数据集非常大,但一开始我尝试只包含我在上面发布的那 15 user/group 个条目,但即使那样也不起作用...我是什么我没看到这里?内存限制已设置为我的计算机的最大值 (4GB),我还执行了帮助功能或任何 R 网站建议的所有操作。

R 版本 3.3.1,平台:x86_64-w64-mingw32/x64

问题是

combn(x,2)

x 是整数时,combn 创建序列 1 ... x 和 returns 来自该序列的所有对,如果 x 很大。如果您有任何组中只有一个用户,就会发生这种情况。

一个解决方案是过滤掉所有只有一个用户的组:

#create pairs
pairs <- do.call(rbind, sapply(Filter(function(x)
    length(x) > 1, split(user, group)), function(x) t(combn(x,2))))