加速独特观察的成对计数

我有一个对象( 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"   

您可以使用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)
链接地址: http://www.djcxy.com/p/92091.html

上一篇: Speed up pairwise counting of unique observations

下一篇: How can I detect dead code after always