I want to tally the number of times that there are consecutive observations matching a condition. For instance. In foo
below I would like to tally the number of days in the month of March where consecutive values of y
are smaller than one standard deviation from the mean value of y
for that month. My data are laid out like foo
:
library(lubridate)
foo <- data.frame(x=seq.Date(as.Date("1981/1/1"),
as.Date("2000/12/31"), "day"))
foo$y <- arima.sim(n = nrow(foo), list(ar = c(0.8)))
I've figured out how to tally the number of days in March for each year where y
is more than one standard deviation below the mean:
bar <- foo %>% filter(month(x) == 3 & y < mean(y)-sd(y)) %>%
group_by(year(x)) %>% tally()
I would like this count to be only when the the days matching the condition are consecutive. E.g., if the mean temperature for March is 0 and and the sd is 1 and March 5, 6 and 7 in the year 1990 are all below -1 the tally would be 3 for the year 1990. If March 21 was also < -1 but March 20 and 22 are not < -1, the tally would still be 3 because March 21 doesn't have neighbors that are also < -1.
I imagine rle
comes into play but I don't understand how.
Any advice appreciated.