2016-06-07 20 views
7

我有一個對象的向量(object)以及觀察對象的相應時間幀向量(tframe)。對於每對唯一的對象,我想計算觀察到兩個對象的時間幀數加速獨特的觀察成對計數

我可以使用for()循環編寫代碼,但需要很長時間才能隨着唯一對象數量的增加而運行。我如何更改代碼以加快運行時間?

下面是一個有4個獨特對象的例子(實際上我有大約300個)。例如,在時間範圍12中都觀察到對象ac,因此他們得到的計數爲2。對象bd從未在同一時間框架內被觀察到,因此他們得到0的計數。

object <- c("a", "a", "a", "b", "b", "c", "c", "c", "c", "d") 
tframe <- c(1, 1, 2, 2, 3, 1, 2, 2, 3, 1) 

uo <- unique(object) 
n <- length(uo) 

mpairs <- matrix(NA, nrow=n*(n-1)/2, ncol=3, dimnames=list(NULL, 
    c("obj1", "obj2", "sametf"))) 

row <- 0 
for(i in 1:(n-1)) { 
for(j in (i+1):n) { 
    row <- row+1 
    mpairs[row, "obj1"] <- uo[i] 
    mpairs[row, "obj2"] <- uo[j] 
    # no. of time frames in which both objects in a pair were observed 
    intwin <- intersect(tframe[object==uo[i]], tframe[object==uo[j]]) 
    mpairs[row, "sametf"] <- length(intwin) 
}} 

data.frame(object, tframe) 
    object tframe 
1  a  1 
2  a  1 
3  a  2 
4  b  2 
5  b  3 
6  c  1 
7  c  2 
8  c  2 
9  c  3 
10  d  1 

mpairs 
    obj1 obj2 sametf 
[1,] "a" "b" "1" 
[2,] "a" "c" "2" 
[3,] "a" "d" "1" 
[4,] "b" "c" "2" 
[5,] "b" "d" "0" 
[6,] "c" "d" "1" 
+1

不知道它是否會更快,但是可以清晰的...'標籤< - tcrossprod(表(唯一的(d)))'得到你想要的計數,所以後來重新排列(如果需要)'tab [lower.tri(tab,TRUE)] < - NA; reshape2 :: melt(tab,na.rm = TRUE)' – user20650

+1

similar [查找具有相同列值的所有序列](http://stackoverflow.com/questions/36827378/find-all-sequences-with-the-same -column-value) – rawr

+0

@ user20650在你的例子中,「d」是什麼? –

回答

4

您可以使用crossproduct來獲得協議計數。如有需要,您可以重新整理 數據。

object <- c("a", "a", "a", "b", "b", "c", "c", "c", "c", "d") 
tframe <- c(1, 1, 2, 2, 3, 1, 2, 2, 3, 1) 

# This will give you the counts 
# Use code from Jean's comment 
tab <- tcrossprod(table(object, tframe)>0) 

# Reshape the data 
tab[lower.tri(tab, TRUE)] <- NA 
reshape2::melt(tab, na.rm=TRUE)