[Solved] Date-time data in R


First convert your dates into a date-time class using asPOSIXct

 df = data.frame(x  = c("2012-03-01T00:05:55+00:00", "2012-03-01T00:06:23+00:00", 
                 "2012-03-01T00:06:52+00:00"))

df$times = as.POSIXct(df$x, format = "%Y-%m-%dT00:%H:%M+%S")

Then extract just the hour part using format

df$hour = format(df$times, '%H')

This give you :

                          x               times hour
1 2012-03-01T00:05:55+00:00 2012-03-01 05:55:00   05
2 2012-03-01T00:06:23+00:00 2012-03-01 06:23:00   06
3 2012-03-01T00:06:52+00:00 2012-03-01 06:52:00   06

Or you can extract the date and the hour using:

df$date_hour = format(df$times, '%Y-%m-%d:%H')

for more infor see ?strftime it says “A conversion specification is introduced by %, usually followed by a single letter or O or E and then a single letter. Any character in the format string not part of a conversion specification is interpreted literally (and %% gives %). Widely implemented conversion specifications include:… %H
Hours as decimal number (00–23). As a special exception strings such as 24:00:00 are accepted for input, since ISO 8601 allows these.”

Now you can do any aggregartion you want using something like plyr::ddply

library(plyr)
ddply(df, .(hour), nrow)

  hour V1
1   05  1
2   06  2

or

ddply(df, .(date_hour), nrow)
      date_hour V1
1 2012-03-01:05  1
2 2012-03-01:06  2

solved Date-time data in R