0

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!

4

1 回答 1

0

由于您已经在使用chron

library(dplyr)
library(chron)
df <- df%>% mutate(Hour = hours(date_time), Value = ifelse((Hour > 7 & Hour < 18), 0, 1))
于 2016-06-28T17:05:38.937 回答