First question to stackoverflow so apologies if I don't follow etiquette...
I have a table df
similar to the following:
subject_id date_time
1 1999-10-20 19:08:00
2 1983-03-16 00:28:14
....
I'm trying to add further columns to flag the day of the event (which I've figured out using wday
) but I need a further flag for whether the event was in working hours (i.e. between 0800 and 1700). So far I can extract the hour from date_time using
df$hour = as.numeric(format(df$date_time, "%H"))
but I'm struggling to add a column to the end of df
which would be conditional 0
(in hours) or 1
based on df$hour
. Could anyone point me in the right direction? Thanks!
Since you are already using chron
:
library(dplyr)
library(chron)
df <- df%>% mutate(Hour = hours(date_time), Value = ifelse((Hour > 7 & Hour < 18), 0, 1))