簡體   English   中英

根據組成員資格數據創建對

[英]Creating pairs from Group Membership Data

首先,對這個可能愚蠢的問題感到抱歉,但是在使用Google並進行跟蹤和錯誤7個小時后,我感到非常沮喪和絕望...

我有一個用戶ID和它們所屬的組的列表。 我需要共享一個組的所有用戶組合的列表(創建網絡圖的邊列表)。 我馬上就發現了這一點 ,並感到非常高興,因為這正是我所需要的。 我以前從未使用過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個用戶/組條目,即使這樣也不起作用...我在這里看不到什么? 內存限制已經設置為我的計算機的最大容量(4GB),並且我也按照幫助功能或建議的任何R網站進行了所有操作。

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

問題是

combn(x,2)

x為整數時, combn創建序列1 ... x並返回該序列中的所有對,如果x大則將是一個巨大的數組。 如果您有任何組中只有一個用戶,就會發生這種情況。

一種解決方案是過濾出只有一個用戶的所有組:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM