'rounding up time in a date-time variable in R
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
