Follow up to this: Create a count-consecutive variable which resets to 1
and the solution worked great. Now I have below, where date is POSixct:
df<-data.frame(group=c(1, 1, 1, 1, 2, 2, 2),
date=c("2000-01-01 00:00:00", "2000-01-03 00:00:00", "2000-01-04 07:07:40", "2000-01-05 09:09:00", "2000-01-09 00:00:00", "2000-01-10 14:00:00", "2000-01-11 13:00:00"),
want=c(1,1,2,3,1,2,1),
want2=c(3,3,3,3,2,2,2))
library(anytime)
df<-df %>% mutate(date = anytime::anytime(str_c(date, sep= '')))
group date want want2
1 1 2000-01-01 00:00:00 1 3
2 1 2000-01-03 00:00:00 1 3
3 1 2000-01-04 07:07:40 2 3
4 1 2000-01-05 09:09:00 3 3
5 2 2000-01-09 00:00:00 1 2
6 2 2000-01-10 14:00:00 2 2
7 2 2000-01-11 13:00:00 1 2
I want to begin counting when the 'next day' is after 24 hrs but before 48 hrs.
Trying this without success, because I think the diff function gives me a result in seconds:
df %>%
group_by(group) %>%
group_by(group2 = cumsum(c(TRUE, diff(date)<86400&diff(date)>172800))), add = TRUE) %>%
mutate(wantn = row_number()) %>%
group_by(group) %>%
mutate(want2n = max(wantn)) %>%
select(-group2)