Home > Software engineering >  How to create intervals of 1 hour
How to create intervals of 1 hour

Time:11-14

How to create for every date hourly timestamps?

So for example from 00:00 til 23:59. The result of the function could be 10:00. I read on the internet that loop could work but we couldn't make it fit.

Data sample:

df = data.frame( id = c(1, 2, 3, 4), Date = c(2021-04-18, 2021-04-19, 2021-04-21
07:07:08.000, 2021-04-22))

CodePudding user response:

A few points:

  • The input shown in the question is not valid R syntax so we assume what we have is the data frame shown reproducibly in the Note at the end.

  • the question did not describe the specific output desired so we will assume that what is wanted is a POSIXct vector of hourly values which in (1) below we assume is from the first hour of the minimum date to the last hour of the maximum date in the current time zone or in (2) below we assume that we only want hourly sequences for the dates in df also in the current time zone.

  • we assume that any times in the input should be dropped.

  • we assume that the id column of the input should be ignored.

No packages are used.

1) This calculates hour 0 of the first date and hour 0 of the day after the last date giving rng. The as.Date takes the Date part, range extracts out the smallest and largest dates into a vector of two components, adding 0:1 adds 0 to the first date leaving it as is and 1 to the second date converting it to the date after the last date. The format ensures that the Dates are converted to POSIXct in the current time zone rather than UTC. Then it creates an hourly sequence from those and uses head to drop the last value since it would be the day after the input's last date.

rng <- as.POSIXct(format(range(as.Date(df$Date))   0:1))
head(seq(rng[1], rng[2], "hour"), -1)

2) Another possibility is to paste together each date with each hour from 0 to 23 and then convert that to POSIXct. This will give the same result if the input dates are sequential; otherwise, it will give the hours only for those dates provided.

with(expand.grid(Date = as.Date(df$Date), hour = paste0(0:23, ":00:00")), 
  sort(as.POSIXct(paste(Date, hour))))

Note

df <- data.frame( id = c(1, 2, 3, 4), 
  Date = c("2021-04-18", "2021-04-19", "2021-04-21 07:07:08.000", "2021-04-22"))
  • Related