I would like to calculate some rainfall indices using a data frame with year, day and the rainfall values.
How to calculate Maximum 5 day Consecutive Precipitation (R5d)? Maximum number of consecutive days with rainfall (rainfall higher than 0mm)? Maximum number of consecutive days without rainfall (rainfall 0 mm)? Annual total rainfall above the 95 percentile?
I tried the followed code for R5d, but it does not work because it sum the days from 1 to 5, from 6 to 10 and so on. I want the sum of days 1 to 5, 2 to 6, 3 to 7 and so on.
year=c(rep(2000,360),rep(2001,360))
day=rep(1:360,2)
rainfall<-rpois(360*2,0:100)
df5=data.frame(year,day,rainfall)
head(df5)
#Sum values 5 by 5
library(dplyr)
sum5=df5 %>%
mutate(Intervals_by5 = rep(row_number(), each=5, length.out = n())) %>%
group_by(Intervals_by5) %>%
summarise(Freq_sum = sum(rainfall))
max(sum5)
Many thanks!
You could use a sliding window per below.
.complete = TRUEif you only want to start summing when you have 4 prior rows.Created on 2024-03-23 with reprex v2.1.0