I currently have a data frame with a time stamp in character form which I converted to HMS
df$Time <- hms(df$Time)
I am trying to mutate the data set to include a categorical column with the time of day
ReceiptNo Time
99 14H 53M 55S
98 14H 53M 43S
97 14H 53M 28S
96 14H 53M 8S
95 14H 52M 53S
94 14H 52M 1S
93 14H 51M 41S
92 14H 51M 15S
91 14H 51M 0S
90 14H 50M 42S
I've tried different variations of the following to mutate a new column with no success
df %>%
mutate(Time = case_when(
Time <= 6 ~ "Night",
Time <= 10 ~ "Morning",
Time <= 14 ~ "Midday",
Time <= 18 ~ "Afternoon",
Time > 19 ~ "Night" ))
Time
is of period
class. Extract the hour from it and use it in case_when
:
library(dplyr)
df %>%
mutate(hour = Time@hour,
Time_of_day = case_when(
hour <= 6 ~ "Night",
hour <= 10 ~ "Morning",
hour <= 14 ~ "Midday",
hour <= 18 ~ "Afternoon",
hour > 19 ~ "Night"))