How to break a 24 hour day into 4 hour time buckets (Chron())

53 Views Asked by At

Using library(chron) Here is the

structure(c(0.876736111111111, 0.876666666666667, 0.876435185185185, 
0.876354166666667, 0.876319444444444, 0.876284722222222, 0.876134259259259, 
0.876122685185185, 0.876006944444444, 0.87587962962963, 0.875717592592593, 
0.875694444444444, 0.875613425925926, 0.875578703703704, 0.875543981481481
), format = "h:m:s", class = "times")

I am trying to separate these Chron() times into buckets and cannot get the ifelse statements to work Sorry for the simple question, I am very new at R and trying to learn!

Here is what I've tried so far:

within(Checklist1, Checklist1$GroupTime <- 
         ifelse(Checklist$Time24 <= .1666666666666666666, 1, 
         ifelse(Checklist$Time24 > .1666666666666666666 && < .33333333333333333333, 2
         ifelse(Checklist$Time24 > .33333333333333333333 && < .50, 3
         ifelse(Checklist$Time24 > .50 & < .66666666666666666666666, 4
         ifelse(Checklist$Time24 >.66666666666666666666666 && < .83333333333333333333333333, 5
         ifelse(Checklist$Time24 > .83333333333333333333333333, 6,NA))) )

The 0.166 is because Chron uses % of the day to calculate the hours

Thanks for the help!

1

There are 1 best solutions below

0
Ronak Shah On BEST ANSWER

I think it is better to use cut/findInterval here instead of nested ifelse statements.

You can try :

Checklist1$GroupTime <- findInterval(Checklist$Time24, c(.1666, .3333, .5, .666,.833)) + 1