Reputation: 251
I'm trying to split a POSIXct variable into three time ranges 18:00 - 21:59, 22:00 - 01:59, and 2:00 - 6:00.
head(Activity$timect)
[1] "2018-06-30 21:51:00 NZST" "2018-06-30 23:14:00 NZST" "2018-06-30 23:39:00 NZST"
[4] "2018-06-30 01:10:00 NZST" "2018-06-30 03:32:00 NZST" "2018-06-30 03:40:00 NZST"
I'm using the package lubridate to do this. The code is as follows:
require(lubridate)
early_activity <- with(Activity, Activity[hour(timect) >= 18 & hour(timect) < 22, ])
mid_activity <- with(Activity, Activity[hour(timect) >= 22 & hour(timect) < 2, ])
late_activity <- with(Activity, Activity[hour(timect) >= 2 & hour(timect) < 6, ])
The problem I'm having is that the early and late activity ranges work fine, but the mid activity time range returns 0 observations although there should be a few hundred.
Any help would be much appreciated!
Upvotes: 0
Views: 36
Reputation: 51
The &
operator will find value that matches both of the statement (both larger than or equal to 22 AND smaller than 2), in this case you should use |
so that it will select anything larger than or equal to 22 OR smaller than 2
require(lubridate)
early_activity <- with(Activity, Activity[hour(timect) >= 18 & hour(timect) < 22, ])
mid_activity <- with(Activity, Activity[hour(timect) >= 22 | hour(timect) < 2, ])
late_activity <- with(Activity, Activity[hour(timect) >= 2 & hour(timect) < 6, ])
Upvotes: 2