将日期和时间添加到事件持续时间

Adding dates and times to event durations

作为 问题的补充,是否可以在另一列中添加事件开始和结束的时间?

这是从 OP 中提取的可重现示例。

df <- structure(list(Time = structure(c(1463911500, 1463911800, 1463912100, 
1463912400, 1463912700, 1463913000), class = c("POSIXct", "POSIXt"
), tzone = ""), Temp = c(20.043, 20.234, 6.329, 20.424, 20.615, 
20.805)), row.names = c(NA, -6L), class = "data.frame")

> df
                 Time   Temp
1 2016-05-22 12:05:00 20.043
2 2016-05-22 12:10:00 20.234
3 2016-05-22 12:15:00  6.329
4 2016-05-22 12:20:00 20.424
5 2016-05-22 12:25:00 20.615
6 2016-05-22 12:30:00 20.805

library(dplyr)
df %>% 
  # add id for different periods/events
  mutate(tmp_Temp = Temp > 20, id = rleid(tmp_Temp)) %>% 
  # keep only periods with high temperature
  filter(tmp_Temp) %>%
  # for each period/event, get its duration
  group_by(id) %>%
  summarise(event_duration = difftime(last(Time), first(Time)))


     id event_duration
  <int> <time>        
1     1  5 mins       
2     3 10 mins 

即还有两列:“start_DateTime”和“end_DateTime”

谢谢!

当然可以。像这样修改最后的 summarise()

df %>% 
  # add id for different periods/events
  mutate(tmp_Temp = Temp > 20, id = rleid(tmp_Temp)) %>% 
  # keep only periods with high temperature
  filter(tmp_Temp) %>%
  # for each period/event, get its duration
  group_by(id) %>%
  summarise(event_duration = difftime(last(Time), first(Time)),
            start_DateTime = min(Time),
            end_DateTime = max(Time))

#> # A tibble: 2 × 4
#>      id event_duration start_DateTime      end_DateTime       
#>   <int> <drtn>         <dttm>              <dttm>             
#> 1     1  5 mins        2016-05-22 12:05:00 2016-05-22 12:10:00
#> 2     3 10 mins        2016-05-22 12:20:00 2016-05-22 12:30:00