user3022875
user3022875

Reputation: 9018

how to use cut to separate dates into 1 hour bins

I have this list of dates for different days:

data = as.POSIXct(c("2017-01-03 06:14:08","2017-01-03 06:15:08","2017-01-03 06:33:07","2017-01-03 06:57:57",
                    "2017-01-03 07:01:08","2017-01-03 07:15:08","2017-01-03 07:33:08","2017-01-03 08:29:08","2017-01-03 08:34:08"),tz = "GMT")

I'd like to create a column that bins the dates into 1 hour buckets starting at 30th minute of the hour so the result would be in the "BUCKET" column below

as.data.frame(data)
                 data   BUCKET
1 2017-01-03 06:14:08   530
2 2017-01-03 06:15:08   530
3 2017-01-03 06:33:07   630
4 2017-01-03 06:57:57   630
5 2017-01-03 07:01:08   630 
6 2017-01-04 07:15:08   630 
7 2017-01-04 07:33:08   730
8 2017-01-05 08:29:08   730
9 2017-01-05 08:34:08   830

Upvotes: 0

Views: 59

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18435

Here is one way to do it...

bucket <- paste0(as.numeric(format(data-60*30,format("%H"))),"30")

bucket
[1] "530" "530" "630" "630" "630" "630" "730" "730" "830"

Upvotes: 2

Related Questions