【发布时间】:2021-10-02 21:28:10
【问题描述】:
我正在寻找一种(最好是 tidyverse)解决方案来创建基于列 direction 的运行计数器(累积和),每次方向更改或停止时都会重置 counter。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
df <- tibble::tribble(
~direction, ~datetime, ~counter,
"Left", "2020-09-27 8:00:00", 1L,
"Left", "2020-09-27 9:00:00", 2L,
"Right", "2020-09-27 10:00:00", 1L,
"Right", "2020-09-27 11:00:00", 2L,
"Right", "2020-09-27 12:00:00", 3L,
"Stop", "2020-09-27 13:00:00", 1L,
"Left", "2020-09-27 14:00:00", 1L,
"Right", "2020-09-27 15:00:00", 1L,
"Left", "2020-09-27 16:00:00", 1L,
"Left", "2020-09-27 17:00:00", 2L
) %>%
mutate(datetime = as_datetime(datetime))
df
#> # A tibble: 10 x 3
#> direction datetime counter
#> <chr> <dttm> <int>
#> 1 Left 2020-09-27 08:00:00 1
#> 2 Left 2020-09-27 09:00:00 2
#> 3 Right 2020-09-27 10:00:00 1
#> 4 Right 2020-09-27 11:00:00 2
#> 5 Right 2020-09-27 12:00:00 3
#> 6 Stop 2020-09-27 13:00:00 1
#> 7 Left 2020-09-27 14:00:00 1
#> 8 Right 2020-09-27 15:00:00 1
#> 9 Left 2020-09-27 16:00:00 1
#> 10 Left 2020-09-27 17:00:00 2
由reprex package (v2.0.0) 于 2021-07-27 创建
我尝试调整来自 this solution 的代码,但无法让它在两次条件重置时工作。
【问题讨论】: