I would love some help calculating the time since the temperature was as cold as it was on a particular date.
So in the example data frame below, for the first record (01/07/2000) the previous time it was as cold as this (-1) was 01/01/2000 (around 182 days before).
for the second record, (01/06/2000) the previous time it was that cold (2 degrees) was the previous month (01/05/2000) where it was actually colder (1 degree) (so around 30 days before).
df <- data.frame(date=as.Date(c("01/07/2000", "01/06/2000", "01/05/2000",
"01/04/2000", "01/03/2000", "01/02/2000",
"01/01/2000"), "%d/%m/%Y"),
temperature =c(-1, 2, 1, 0, 1, 1, -1))
I have tried modifying this approach (Calculate days since last event in R) but found it became unwieldy when calculating for each week.
Any ideas how you might calculate the number of days since the weather was that cold, for each week? Many thanks, indeed for your help.