基于R中的时间戳的快速数据填充

时间:2017-01-02 11:25:05

标签: r matching

祝大家新年快乐

来到我的问题:

我有两个数据集。

Dataset 1

    Time            Name            Value
    6/1/2016 9:39   ABCD IS Equity  11.01
    6/1/2016 9:44   ABCD IS Equity  11.05
    6/1/2016 9:46   ABCD IS Equity  11.01
    6/1/2016 9:58   ABCD IS Equity  11.01
    6/1/2016 10:10  ABCD IS Equity  11.01
    6/1/2016 10:13  ABCD IS Equity  11.01
    6/1/2016 10:33  ABCD IS Equity  11.02
    6/1/2016 10:42  ABCD IS Equity  11.02
    6/1/2016 10:52  ABCD IS Equity  11.02
    6/1/2016 10:56  ABCD IS Equity  11.06
    6/1/2016 11:14  ABCD IS Equity  11.02
    6/1/2016 11:25  ABCD IS Equity  11.03
    6/1/2016 11:26  ABCD IS Equity  11.03
    6/1/2016 11:29  ABCD IS Equity  11.03
    6/1/2016 11:30  ABCD IS Equity  11.03
    6/1/2016 11:40  ABCD IS Equity  11.03
    6/1/2016 11:40  ABCD IS Equity  11.01
    6/1/2016 11:44  ABCD IS Equity  11.01
    6/1/2016 12:04  ABCD IS Equity  11.01

Dataset 2

Time2           Name2           Value2
6/1/2016 9:42   ABCD IS Equity  123
6/1/2016 9:45   ABCD IS Equity  124
6/1/2016 9:45   ABCD IS Equity  125
6/1/2016 10:00  ABCD IS Equity  126
6/1/2016 10:14  ABCD IS Equity  127
6/1/2016 10:14  ABCD IS Equity  128
6/1/2016 10:14  ABCD IS Equity  129
6/1/2016 10:41  ABCD IS Equity  130
6/1/2016 10:45  ABCD IS Equity  131
6/1/2016 10:56  ABCD IS Equity  132
6/1/2016 10:58  ABCD IS Equity  133
6/1/2016 11:26  ABCD IS Equity  134
6/1/2016 11:27  ABCD IS Equity  135
6/1/2016 11:30  ABCD IS Equity  136
6/1/2016 11:32  ABCD IS Equity  137
6/1/2016 11:40  ABCD IS Equity  138
6/1/2016 11:42  ABCD IS Equity  139
6/1/2016 11:45  ABCD IS Equity  140
6/1/2016 12:05  ABCD IS Equity  141

现在,我想在New中创建一个Dataset 1列,根据条件Value2Dataset2填充每个Dataset2$Time2 > Dataset1$Time的值。 Dataset 1

以下是示例output

Time            Name            Value   New
6/1/2016 9:39   ABCD IS Equity  11.01   123
6/1/2016 9:44   ABCD IS Equity  11.05   124
6/1/2016 9:46   ABCD IS Equity  11.01   126
6/1/2016 9:58   ABCD IS Equity  11.01   126
6/1/2016 10:10  ABCD IS Equity  11.01   127
6/1/2016 10:13  ABCD IS Equity  11.01   127
6/1/2016 10:33  ABCD IS Equity  11.02   130
6/1/2016 10:42  ABCD IS Equity  11.02   131
6/1/2016 10:52  ABCD IS Equity  11.02   132
6/1/2016 10:56  ABCD IS Equity  11.06   133
6/1/2016 11:14  ABCD IS Equity  11.02   134
6/1/2016 11:25  ABCD IS Equity  11.03   134
6/1/2016 11:26  ABCD IS Equity  11.03   135
6/1/2016 11:29  ABCD IS Equity  11.03   136
6/1/2016 11:30  ABCD IS Equity  11.03   137
6/1/2016 11:40  ABCD IS Equity  11.03   139
6/1/2016 11:40  ABCD IS Equity  11.01   139
6/1/2016 11:44  ABCD IS Equity  11.01   140
6/1/2016 12:04  ABCD IS Equity  11.01   141

Value2中的相同值可能会根据匹配条件填充在Dataset1的不同行中。

SOLN。我试过了:

我尝试使用简单的for循环[1: nrow(Dataset1)]来匹配Dataset2的每一行。但我有一个庞大的数据集,需要很长时间。我正在寻找更快的方法 - 可以跳过使用for循环。

任何帮助/建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

可能的选项是来自findInterval

base R
 df2$New <- df2$Value2[findInterval(df1$Time, df2$Time2)+1]

注意:我们假设'Time','Time2'是POSIXct class