在r中的日期时间变量中汇总时间

发布于 2025-01-26 10:01:08 字数 1158 浏览 2 评论 0原文

我一直在寻找解决问题的解决方案,我认为很容易解决,但似乎并非如此。我正在尝试将DateTime变量汇总到接下来的5分钟。

Incidents<- structure(list(Event.Id = c(240252L, 240258L, 240261L, 240264L, 
240294L, 240313L, 240314L, 240315L, 240331L, 240343L, 240350L, 
240358L, 240379L, 240381L, 240396L), DateTime = structure(c(1427868300, 
1427872800, 1427873100, 1427873700, 1427881500, 1427890260, 1427890860, 
1427890980, 1427900160, 1427902860, 1427904480, 1427906700, 1427910900, 
1427911920, 1427919360), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(5L, 
11L, 14L, 15L, 40L, 59L, 60L, 61L, 77L, 90L, 96L, 103L, 122L, 
123L, 135L), class = "data.frame")

我用过:

Incidents$DateTime2<- round_date(Incidents$DateTime,unit="5 minutes")

但是,这是最接近5分钟的时间。我想要接下来的5分钟。 我在下面尝试了此代码:

Incidents<- Incidents %>% mutate(time = as.numeric(substr(DateTime, 16,16)))
Incidents$time <- ifelse(Incidents$time <6,5- Incidents$time, 10 - Incidents$time)
minut<- minutes(5)
Incidents$DateTime3<- ifelse(Incidents$time <3, Incidents$DateTime2 + minut, Incidents$DateTime2)

但是,这给了我无法将其转换为日期时间变量的数字值。

I have been looking for a solution for a problem I thought is easy to solve but it doesn't seem so. I am trying roundup a DateTime Variable to the next 5 minutes.

Incidents<- structure(list(Event.Id = c(240252L, 240258L, 240261L, 240264L, 
240294L, 240313L, 240314L, 240315L, 240331L, 240343L, 240350L, 
240358L, 240379L, 240381L, 240396L), DateTime = structure(c(1427868300, 
1427872800, 1427873100, 1427873700, 1427881500, 1427890260, 1427890860, 
1427890980, 1427900160, 1427902860, 1427904480, 1427906700, 1427910900, 
1427911920, 1427919360), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(5L, 
11L, 14L, 15L, 40L, 59L, 60L, 61L, 77L, 90L, 96L, 103L, 122L, 
123L, 135L), class = "data.frame")

I used:

Incidents$DateTime2<- round_date(Incidents$DateTime,unit="5 minutes")

However, this rounds the time to the closest 5 minutes. I want the next 5 minutes instead.
I tried this code below:

Incidents<- Incidents %>% mutate(time = as.numeric(substr(DateTime, 16,16)))
Incidents$time <- ifelse(Incidents$time <6,5- Incidents$time, 10 - Incidents$time)
minut<- minutes(5)
Incidents$DateTime3<- ifelse(Incidents$time <3, Incidents$DateTime2 + minut, Incidents$DateTime2)

However, this gives me numeric values that I could not convert to date time variable.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文