I was trying to sum numbers whose time lag is 1. i.e. I would like to summarize the rows by adding the frequencies of values where the days differ only by a single day within a particular group. I used the lag function to get the diff, but not sure how to proceed from here.
df <- df %>%
group_by(group) %>%
mutate(diff = dt - lag(dt))
df[!is.na(df$diff) & df$diff > 1,]$diff <- NA
For ex:
group dt freq diff
groupA 2016-03-21 1 NA
groupA 2016-03-22 1 1
groupA 2016-03-23 1 1
groupA 2016-03-26 2 NA
groupA 2016-03-28 1 NA
groupA 2016-03-29 3 1
groupA 2016-03-30 3 1
groupA 2016-03-31 5 1
groupB 2016-04-01 1 NA
groupB 2016-04-02 2 1
I need to group this into:
group dt freq diff duration
groupA 2016-03-21 1 NA 3 (1 + 1 + 1)
groupA 2016-03-22 1 1
groupA 2016-03-23 1 1
groupA 2016-03-26 2 NA 2
groupA 2016-03-28 1 NA 12(1 + 3 + 3 + 5)
groupA 2016-03-29 3 1
groupA 2016-03-30 3 1
groupA 2016-03-31 5 1
groupB 2016-04-01 1 NA 3(1 + 2)
groupB 2016-04-02 2 1
Also referred to this, but cumulative does not work as I do not consider jumps more than a single day apart. Is looping in a custom function the only way?