将日期和时间添加到活动持续时间

发布于 2025-01-29 15:17:18 字数 1143 浏览 3 评论 0原文

作为这个问题的补充,是否可以添加事件启动时以及在另一列中完成的可以添加?

这是一个从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”

谢谢!

As an addition to this question, is it possible to add when an event started and when it finished in another column(s)?

Here is a reproducible example pulled from the 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 

i.e there are two more columns: "start_DateTime" and "end_DateTime"

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-02-05 15:17:19

当然。修改最终summarize()之类的:

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

Sure. Modify the final summarise() like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文