Mr369
Mr369

Reputation: 394

R create time interval using cut.Date

my data frame looks like:

set.seed(1) 
MyDates <- ISOdatetime(2012, 1, 1, 0, 0, 0, tz = "GMT") + sample(1:27000, 500)

It is fair easy to count number of observations per 5 mins by using cut

df <- data.frame(table(cut(MyDates, breaks = "5 mins")))

It gives me intervals as

00:00:00 -- 00:05:00, 
00:05:00 -- 00:10:00

But how about if I want to get a 'customized' intervals as

00:00:00 -- 00:05:00, 
00:01:00 -- 00:06:00,  
00:02:00 -- 00:07:00

Any help would be appreciated!

Upvotes: 1

Views: 930

Answers (1)

Nicol&#225;s Velasquez
Nicol&#225;s Velasquez

Reputation: 5908

You just need to pass a vector of numeric values to cut.

An example:

data.frame(table(cut(MyDates, 
                     c(min(MyDates),         ## "Leftmost" cut
                       min(MyDates) + 5000,  ## Custom cut 1
                       min(MyDates) + 17000, ## Custom cut 2
                       min(MyDates) + 65000),## "Rightmost" cut
                     right = TRUE)))         ## let cut() know where should the infinite be closed or open.

Upvotes: 1

Related Questions