R data.table左外连接,匹配最近

时间:2018-01-22 04:33:47

标签: r data.table left-join

我想通过最接近的时间戳加入两张桌子。有些时间戳完全匹配,但大多数时间戳都没有。我可以找到herehere的大量数据示例。

表1:eyeDatP1_final

+--------+---------+---------+----------+----------+--------+--------------------+-------------+
| "team" | "block" | "trial" | "x.pos1" | "y.pos1" | "time" |      "time1"       | "condition" |
+--------+---------+---------+----------+----------+--------+--------------------+-------------+
|    910 |     1   |       1 |    7.012 |   19.034 |      0 |                  0 | "normal"    |
|    910 |     1   |       1 |    7.012 |   19.034 |      0 | 0.0900001525878906 | "normal"    |
|    910 |     1   |       1 |    7.012 |   19.034 |      0 |  0.180000066757202 | "normal"    |
|    910 |     1   |       1 |    7.012 |   19.034 |      0 |  0.279999971389771 | "normal"    |
|    910 |     1   |       1 |    7.012 |   19.034 |      0 |  0.370000123977661 | "normal"    |
+--------+---------+---------+----------+----------+--------+--------------------+-------------+

表2:eyeDatP2_final

+--------+---------+---------+----------+----------+-------------------+-------------------+-------------+
| "team" | "block" | "trial" | "x.pos2" | "y.pos2" |      "time"       |      "time2"      | "condition" |
+--------+---------+---------+----------+----------+-------------------+-------------------+-------------+
|    910 |       1 |       1 |   72.656 | -136.216 |                 0 |                 0 | "normal"    |
|    910 |       1 |       1 |   71.398 | -139.887 | 0.070000171661377 | 0.070000171661377 | "normal"    |
|    910 |       1 |       1 |   68.219 | -146.002 | 0.170000076293945 | 0.170000076293945 | "normal"    |
|    910 |       1 |       1 | -166.067 |  125.859 | 0.259999990463257 | 0.259999990463257 | "normal"    |
|    910 |       1 |       1 | -164.175 |  112.627 | 0.360000133514404 | 0.360000133514404 | "normal"    |
+--------+---------+---------+----------+----------+-------------------+-------------------+-------------+

编辑,此代码有效:

eyeDatP1_final <- setDT(eyeDatP1_final)
eyeDatP2_final <- setDT(eyeDatP2_final)
eyeDatP1_final$time.matched <- eyeDatP1_final$time1
eyeDatP2_final$time.matched <- eyeDatP1_final$time2
setkey(eyeDatP1_final, team, condition, trial, time.match)
setkey(eyeDatP2_final, team, condition, trial, time.match)
eyeDat_final <- eyeDatP1_final[eyeDatP2_final, roll = "nearest"]

0 个答案:

没有答案