这是一个玩具数据。
datetime <- c('2019-11-06 13:05:07',
'2019-11-06 13:05:08',
'2019-11-06 13:05:09',
'2019-11-06 13:05:10',
'2019-11-06 13:05:11',
'2019-11-06 13:05:12',
'2019-11-06 13:05:13',
'2019-11-06 13:05:14',
'2019-11-06 13:05:15',
'2019-11-06 13:05:16',
'2019-11-06 13:05:17',
'2019-11-06 13:05:18',
'2019-11-06 13:05:19',
'2019-11-06 13:05:20',
'2019-11-06 13:05:21',
'2019-11-06 13:05:22')
active <- c(1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0)
df <- data.frame(datetime, active)
我正在寻找可以计算活动块的矢量化代码。 在上面的示例中,我们有2个活动块。
- 13:05:07-13:05:10
- 13:05:16-13:05:20
答案 0 :(得分:2)
aggregate(as.POSIXct(datetime) ~ id,
subset(transform(df, id = with(rle(df$active), rep(seq_along(lengths), lengths))),
active == 1),
function(x) toString(range(x)))
# id lubridate::ymd_hms(datetime)
#1 1 2019-11-06 13:05:07, 2019-11-06 13:05:10
#2 3 2019-11-06 13:05:16, 2019-11-06 13:05:20
OR
library(dplyr)
library(data.table)
df %>%
mutate(datetime = as.POSIXct(datetime),
id = rleid(active)) %>%
filter(active == 1) %>%
group_by(id) %>%
summarise(min = min(datetime),
max = max(datetime))
## A tibble: 2 x 3
# id min max
# <int> <dttm> <dttm>
#1 1 2019-11-06 13:05:07 2019-11-06 13:05:10
#2 3 2019-11-06 13:05:16 2019-11-06 13:05:20