Search code examples
rdatehour

How to add hourly rows on a daily sequence dataframe?


if i have a daily data in the following format:

A:
    DD-MM-YYYY
    01-01-2000
    02-01-2000
    03-01-2000
    04-01-2000
    ...
    31-12-2010
    31-12-2010
    31-12-2010
    31-12-2010

How to add hourly values to all the days and obtain a new A like:

A:
    DD-MM-YYYY hour
    01-01-2000 00:00
    01-01-2000 01:00
    01-01-2000 02:00
    01-01-2000 03:00
    ...
    01-01-2000 21:00
    01-01-2000 22:00
    01-01-2000 23:00

    ...
    ...
    31-12-2010 21:00
    31-12-2010 22:00
    31-12-2010 23:00

Solution

  • This will stick 00:00 to 23:00 on to each of your days:

    expand.grid(day = A$`DD-MM-YYYY`, hour = sprintf("%02d:00", 0:23))
    

    However, in the real world you might prefer to use seq.POSIXt, which will account for leap years, daylight savings, etc.