I want to calculate the sum of the flooded days for a variable intervall of time. the end of the period is given in a vector or data.frame and I want different length of my time period, e. g. 4 days and 6 days.
How can I create a code, which is flexible, so that I can calculate different date_end's and also create a vector with different length of the period?
My complete data.frame contains about 2 years, and 12 end dates and 3 different period lenght.
df <- data.frame(date = c("2016-11-01", "2016-11-02", "2016-11-03", "2016-11-04", "2016-11-05", "2016-11-06", "2016-11-07", "2016-11-08", "2016-11-09", "2016-11-10"),
flooded = c(0,0,0,1,1,1,0,0,1,1))
date_end <- as.Date(c("2016-11-04", "2016-11-10"), "%Y-%m-%d")
##lenght of time period, e. g. 4 days
period <- c(4,6)
date flooded
1 2016-11-01 0
2 2016-11-02 0
3 2016-11-03 0
4 2016-11-04 1
5 2016-11-05 1
6 2016-11-06 1
7 2016-11-07 0
8 2016-11-08 0
9 2016-11-09 1
10 2016-11-10 1
All in all I want to calculate the flooded days of my obersavtion point. Thank you