使用 apply 比较两个数据帧之间的所有与所有比较

all vs. all comparisons between two dataframes using apply

我正在使用 R,我有三个数据帧,它们有两个相同的列(ID 和时间戳),但行数不同。

ID       timeStamp
a   2018-04-17 10:47:45
a   2018-04-17 10:47:48
a   2018-04-17 10:47:48
a   2018-04-17 10:47:48
a   2018-04-17 10:49:23
a   2018-04-17 10:50:02
a   2018-04-17 10:51:34
a   2018-04-17 10:51:36
a   2018-04-17 10:51:38


ID       timeStamp
b   2018-04-17 10:32:17
b   2018-04-17 10:46:18
b   2018-04-17 10:47:18
b   2018-04-17 10:49:20
b   2018-04-17 10:52:22
b   2018-04-17 10:55:25
b   2018-04-17 10:57:29

ID       timeStamp
c   2018-04-17 10:32:17
c   2018-04-17 10:46:18
c   2018-04-17 10:47:18
c   2018-04-17 10:49:20
c   2018-04-17 10:52:22
c   2018-04-17 10:55:25

我想比较三个数据帧中的所有时间戳值,并根据数据帧 A、B 和 C 中的观察次数在特定时间范围内计算点。例如,如果两个 obs 在 5 分钟范围内,我想分配 10 分。如果值完全相同,它将获得 5 分。否则不加分。我想得到两个数据帧之间的总分

我尝试使用 for 循环制作模型,但是当我比较大量行时需要很长时间。

m= 0
n= 0
for (i in 1:nrow(A)){
  for (j in 1:nrow(B)){if (difftime(A[i,"tStamp"],B[j,"tStamp"],units = "secs") < 300 & A(Role1[i,"tStamp"],B[j,"tStamp"],units = "secs") >0 ) {m=m+10}
else if ( difftime(A[i,"tStamp"],B[j,"tStamp"],units = "secs") == 0){m=m+5}
else if (difftime(B[j,"tStamp"],A[i,"tStamp"],units = "secs") < 300 & difftime(B[j,"tStamp"],A[i,"tStamp"],units = "secs") >0) {n=n+10}
else if ( difftime(B[j,"tStamp"],A[i,"tStamp"],units = "secs") == 0){n=n+5}}

有没有使用apply函数的好方法?我相信它会比 for 循环更有效率和更快。预期的输出将像

ID1     ID2               m              n
A        B     
A        C      
B        C   

m和n将是循环后两个数据帧关系的总加分。 任何帮助将不胜感激。

我不确定您要寻找哪种最终输出,但首先您可以使用 outerdifftime

mat <- outer(df1$timeStamp, df2$timeStamp, difftime, units = 'mins')
mat

#Time differences in mins
#      [,1] [,2] [,3]  [,4]   [,5]  [,6]  [,7]
# [1,] 15.5 1.45 0.45 -1.58 -4.617 -7.67 -9.73
# [2,] 15.5 1.50 0.50 -1.53 -4.567 -7.62 -9.68
# [3,] 15.5 1.50 0.50 -1.53 -4.567 -7.62 -9.68
# [4,] 15.5 1.50 0.50 -1.53 -4.567 -7.62 -9.68
# [5,] 17.1 3.08 2.08  0.05 -2.983 -6.03 -8.10
# [6,] 17.8 3.73 2.73  0.70 -2.333 -5.38 -7.45
# [7,] 19.3 5.27 4.27  2.23 -0.800 -3.85 -5.92
# [8,] 19.3 5.30 4.30  2.27 -0.767 -3.82 -5.88
# [9,] 19.4 5.33 4.33  2.30 -0.733 -3.78 -5.85

从这里开始,您可能希望取可用值的绝对值并将小于 5 的值转换为 10,然后执行相应的计算。