Binning time series in R?

binning, r, timestamp

Solution

While you could convert to a formal time representation, in this case it might be easier to just use `substr`:

test <- c("00:00:01","02:07:01","22:30:15")
as.numeric(substr(test,1,2))
#[1]  0  2 22

Using a `POSIXct` time to deal with it would also work, and might be handy if you plan on further calculations (differences in time etc):

testtime <- as.POSIXct(test,format="%H:%M:%S")
#[1]"2013-12-09 00:00:01 EST" "2013-12-09 02:07:01 EST" "2013-12-09 22:30:15 EST"
as.numeric(format(testtime,"%H"))
#[1]  0  2 22

Problem

I'm new to R. My data has 600k objects defined by three attributes: `Id`, `Date` and `TimeOfCall`. `TimeofCall` has a `00:00:00` format and range from `00:00:00` to `23:59:59`. I want to bin the `TimeOfCall` attribute, into 24 bins, each one representing hourly slot (first bin `00:00:00` to `00:59:59` and so on). Can someone talk me through how to do this? I tried using `cut()` but apparently my format is not numeric. Thanks in advance!

Original source

Related problems