I have a DateTime vector within a data.frame where the data frame is made up of 8760 observations representing hourly intervals throughout the year e.g.
2010-01-01 00:00
2010-01-01 01:00
2010-01-01 02:00
2010-01-01 03:00
and so on.
I would like to create a data.frame which has the original DateTime vector as the first column and then the hourly values in the second column e.g.
2010-01-01 00:00 00:00
2010-01-01 01:00 01:00
How can this be achieved?
On a more general note, for anyone that comes here from google and maybe wants to group by hour:
The key here is:
lubridate::hour(datetime)
p22 in the cran doc here: https://cran.r-project.org/web/packages/lubridate/lubridate.pdf
Or generically, not treating them as dates, you can use the following provided that the time and dates are padded correctly.
It depends on your needs really.
Using
lubridate
Use
format
orstrptime
to extract the time information.Create a POSIXct vector:
Extract the time:
If the input vector is a character vector, then you have to convert to POSIXct first:
Create some data
Split time out: