I have a dataset with date and time variables. I created a new variable (mutated from time) and called it "time.of.day". I would like to assign different labels (4 actually) conditioned by the time period. I was trying the following:
levels(df$time.of.day) <- list(
label_1 = df$time.of.day[df$time >= "07:00:00" & df$time <= "10:00:00"],
label_2 = df$time.of.day[df$time >= "10:00:00" & df$time <= "16:00:00"],
label_3 = df$time.of.day[df$time >= "16:00:00" & df$time <= "19:00:00"],
label_4 = df$time.of.day[df$time >= "19:00:00" & df$time <= "23:59:59"]
)
but nothing happens and I get no errors or warnings.
Here's a sample of the mentioned columns:
date time time.of.day
1 2014-03-21 09:20:08 09:20:08
2 2014-03-21 10:05:22 10:05:22
3 2014-03-26 05:34:04 05:34:04
4 2014-03-26 09:35:05 09:35:05
5 2014-03-27 01:45:03 01:45:03
6 2014-03-27 02:45:27 02:45:27
7 2014-03-27 14:46:26 14:46:26
8 2014-03-28 04:03:30 04:03:30
To make it easier for future users here's the code to generate the data frame above:
df <- data.frame(
date = c("2014-03-21", "2014-03-21", "2014-03-26", "2014-03-26", "2014-03-27", "2014-03-27", "2014-03-27", "2014-03-28"),
time = c("09:20:08", "10:05:22", "05:34:04", "09:35:05", "01:45:03", "02:45:27", "14:46:26", "04:03:30"),
time.of.day = c("09:20:08", "10:05:22", "05:34:04", "09:35:05", "01:45:03", "02:45:27", "14:46:26", "04:03:30")
)
P.S.: I have done this in previous work with unique, grep & character strings and it works.
Could you please help? Thanks