Speed up pairwise counting of unique observations

I have a vector of objects ( object ) along with a corresponding vector of time frames ( tframe ) in which the objects were observed. For each unique pair of objects, I want to calculate the number of time frames in which both objects were observed.

I can write the code using for() loops, but it takes a long time to run as the number of unique objects increases. How might I change the code to speed up the run time?

Below is an example with 4 unique objects (in reality I have about 300). For example, objects a and c were both observed in time frames 1 and 2 , so they get a count of 2 . Objects b and d were never observed in the same time frame, so they get a count of 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"   

You can use crossproduct to get the counts of agreement. You can then reshape the data, if required.

Example

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/92092.html

上一篇: Rx.Net消息解析器

下一篇: 加速独特观察的成对计数