保留dplyr中当前变异变量的上一行值

时间:2017-09-06 15:00:45

标签: r dplyr mutate

下面是我的数据集,我正在尝试使用dplyr中当前突变值的前一行值。 first(start_time)不起作用,因为在此代码之前发生了group_by,因此第一个引用了组中的第一个不是我想要的。

这是我正在尝试的代码:

mutate(shift_start_time = if_else(diff==0 | diff > 21600,start_time,lag(shift_start_time)))

但是shift_start_time是当前变异的变量无法进行操作,我正在努力进行任何其他工作。

电流:

# A tibble: 703 × 4
            start_time            end_time      diff    shift_start_time
                <dttm>              <dttm>    <time>              <dttm>
1  2017-05-01 14:21:00 2017-05-01 14:42:18    0 secs 2017-05-01 14:21:00
2  2017-05-01 14:48:00 2017-05-01 15:08:17  342 secs 1970-01-01 00:00:00
3  2017-05-01 15:14:00 2017-05-01 15:35:31  343 secs 1970-01-01 00:00:00
4  2017-05-01 15:42:00 2017-05-01 15:45:36  389 secs 1970-01-01 00:00:00
5  2017-05-01 15:50:00 2017-05-01 16:51:47  264 secs 1970-01-01 00:00:00
6  2017-05-01 16:39:00 2017-05-01 16:51:22 -767 secs 1970-01-01 00:00:00
7  2017-05-01 17:37:00 2017-05-01 17:51:57 2738 secs 1970-01-01 00:00:00
8  2017-05-01 17:46:00 2017-05-01 17:58:25 -357 secs 1970-01-01 00:00:00
9  2017-05-01 17:59:00 2017-05-01 18:11:16   35 secs 1970-01-01 00:00:00
10 2017-05-01 18:08:00 2017-05-01 18:22:22 -196 secs 1970-01-01 00:00:00

必需:

# A tibble: 703 × 4
            start_time            end_time      diff    shift_start_time
                <dttm>              <dttm>    <time>              <dttm>
1  2017-05-01 14:21:00 2017-05-01 14:42:18    0 secs 2017-05-01 14:21:00
2  2017-05-01 14:48:00 2017-05-01 15:08:17  342 secs 2017-05-01 14:21:00
3  2017-05-01 15:14:00 2017-05-01 15:35:31  343 secs 2017-05-01 14:21:00
4  2017-05-01 15:42:00 2017-05-01 15:45:36  389 secs 2017-05-01 14:21:00
5  2017-05-01 15:50:00 2017-05-01 16:51:47  264 secs 2017-05-01 14:21:00
6  2017-05-01 16:39:00 2017-05-01 16:51:22 -767 secs 2017-05-01 14:21:00
7  2017-05-01 17:37:00 2017-05-01 17:51:57 2738 secs 2017-05-01 14:21:00
8  2017-05-01 17:46:00 2017-05-01 17:58:25 -357 secs 2017-05-01 14:21:00
9  2017-05-01 17:59:00 2017-05-01 18:11:16   35 secs 2017-05-01 14:21:00
10 2017-05-01 18:08:00 2017-05-01 18:22:22 -196 secs 2017-05-01 14:21:00

0 个答案:

没有答案