我有一个带有时间戳的数据集,如下所示
t = c("2006-01-02 09:09:38 UTC", "2006-01-04 08:45:34 UTC", "2006-01-10 12:55:41 UTC",
"2006-01-20 09:33:54 UTC", "2006-02-02 11:36:06 UTC", "2006-02-15 08:51:03 UTC",
"2007-06-07 16:26:56 UTC", "2008-04-01 13:20:20 UTC", "2008-04-03 10:53:50 UTC")
seller = c("A", "E", "B", "B", "C", "F", "A", "B", "E")
buyer = c("B", "F", "A", "C", "B", "G", "C", "A", "G")
data = cbind.data.frame(t, seller, buyer)
我想计算所有可能的两条路径的传递闭合时间。这就是我的意思:
我有两条两条路径,即
A-> B,B-> C
E-> F,F-> G
我想计算观察时间
A-> C
E-> G
所需的输出应为:
# 1. List with all the two-paths structures that ultimately produce a transitive closure
out1 = list(o1 = data[c(1,4,7), ], o2 = data[c(2,6,9), ])
out1
$o1
t seller buyer
1 2006-01-02 09:09:38 UTC A B
4 2006-01-20 09:33:54 UTC B C
7 2007-06-07 16:26:56 UTC A C
$o2
t seller buyer
2 2006-01-04 08:45:34 UTC E F
6 2006-02-15 08:51:03 UTC F G
9 2008-04-03 10:53:50 UTC E G
# 2. Time to transitive closure
out2 = list(o1 = difftime(out1$o1[3,1], out1$o1[1,1]), o2 = difftime(out1$o2[3,1], out1$o2[1,1]))
out2
$o1
Time difference of 521.262 days
$o2
Time difference of 820.0474 days
感谢您的帮助!