Reputation: 35
I have observations for several days: three dates and continuous time (00:00:00 - 23:59:59) in the format "yyyy-mm-dd %H:%M:%S". I want to create categorical variable corresponding to 23 breaks (hours of day), i.e. 00:00-01:00, 01:00-02:00 ,..., 23:00-00:00. So that the datetime "yyyy-mm-dd 22:51:03" is converted to 22:00-23:00. cut.POSIXct
gives breaks for hours of each day separately
cut.POSIXct(df$datetime, breaks = "hours", include.lowest = T) # factor variable of 69 levels
or breaks for days
cut.POSIXct(df$datetime, breaks = "days", include.lowest = T) # factor variable of 3 levels
How can I get factor variable of 23 levels?
Upvotes: 1
Views: 393
Reputation: 388982
If datetime
is already in POSIXct
format, one way would be to extract the hours from each value, convert them to factor
and then to integer
so that you will have one level for each hour irrespective of the date.
df$level <- as.integer(factor(format(df$datetime, "%H")))
If datetime
is not in POSIXct
you might want to change it first
df$datetime <- as.POSIXct(df$datetime)
Upvotes: 1