我目前正试图想出一种方法,将一些数据从一个数据帧粘贴到另一个数据帧的开始时间和结束时间
假设我有数据集叫它testsubset1
Event FlowRate
1 2013-05-25 17:29:31 | 0.3739769
2 2013-05-25 17:37:31 | 0.5208873
3 2013-05-25 17:39:01 | 0.4235871
20 2013-05-26 01:16:31 | 0.3010403
21 2013-05-26 01:38:41 | 0.3054283
22 2013-05-26 02:01:01 | 0.3919175
116 2013-05-28 10:58:11 | 0.3851580
117 2013-05-28 11:11:12 | 0.3981671
118 2013-05-28 11:16:21 | 0.4075771
253 2013-05-31 08:31:11 | 0.3543576
254 2013-05-31 08:53:21 | 0.3553817
我还有另一个数据集称之为Flow Obs
Start Finish FlowObs
1 2013-05-25 17:29:00 | 2013-05-26 18:38:00 | 0.3307309
2 2013-05-27 16:22:00 | 2013-05-28 20:15:00 | 0.3286909
3 2013-05-29 13:05:00 | 2013-05-30 14:42:00 | 0.3211857
4 2013-05-30 15:08:00 | 2013-06-03 11:54:00 | 0.3277443
现在我想根据开始和结束时间将Flow Obs中第3列的元素绑定到大数据,以便最终数据集看起来像
Event FlowRate FlowObs
1 2013-05-25 17:29:31 | 0.3739769 | 0.3307309
2 2013-05-25 17:37:31 | 0.5208873 | 0.3307309
3 2013-05-25 17:39:01 | 0.4235871 | 0.3307309
20 2013-05-26 01:16:31 | 0.3010403 | 0.3307309
21 2013-05-26 01:38:41 | 0.3054283 | 0.3307309
22 2013-05-26 02:01:01 | 0.3919175 | 0.3307309
116 2013-05-28 10:58:11 | 0.3851580 | 0.3286909
117 2013-05-28 11:11:12 | 0.3981671 | 0.3286909
118 2013-05-28 11:16:21 | 0.4075771 | 0.3286909
253 2013-05-31 08:31:11 | 0.3543576 | 0.3277443
254 2013-05-31 08:53:21 | 0.3553817 | 0.3277443
逻辑是,如果事件处于Flow obs的开始和结束之间,它会将FlowOb绑定到testsubset。
我确信有一种聪明的方法可以使用和应用函数,但我无法完全理解它。
我已经尝试了一个完成此操作的for循环,但无法找到适当的方法来介入较小的数据框
希望这个问题有道理。关于Stack Over flow的问题我还是新手。
作为旁注,时间是POSIX,流程都是数字。
编辑:我甚至尝试过这样做:
testsubset1[(testsubset1$Event) %in% (c(flowobs[[1]][1], flowobs[[1]][2])),]
然后返回
[1] Event FlowRate
<0 rows> (or 0-length row.names)
答案 0 :(得分:2)
使用sqldf:
library(sqldf)
sqldf("select * from testsubset1 t, FlowObs f
where t.Event between f.Start and f.Finish")
给出:
Event FlowRate Start Finish FlowObs
1 2013-05-25 17:29:31 0.3739769 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
2 2013-05-25 17:37:31 0.5208873 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
3 2013-05-25 17:39:01 0.4235871 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
4 2013-05-26 01:16:31 0.3010403 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
5 2013-05-26 01:38:41 0.3054283 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
6 2013-05-26 02:01:01 0.3919175 2013-05-25 17:29:00 2013-05-26 18:38:00 0.3307309
7 2013-05-28 10:58:11 0.3851580 2013-05-27 16:22:00 2013-05-28 20:15:00 0.3286909
8 2013-05-28 11:11:12 0.3981671 2013-05-27 16:22:00 2013-05-28 20:15:00 0.3286909
9 2013-05-28 11:16:21 0.4075771 2013-05-27 16:22:00 2013-05-28 20:15:00 0.3286909
10 2013-05-31 08:31:11 0.3543576 2013-05-30 15:08:00 2013-06-03 11:54:00 0.3277443
11 2013-05-31 08:53:21 0.3553817 2013-05-30 15:08:00 2013-06-03 11:54:00 0.3277443