I have daily time-series data for 60 years about the presence and absence of rainfall for 400 stations. The data is in the following format where, in the second column, 1 indicates presence and 0 indicate absence:
Date         Rainfall
---------------------
1981-01-01   0
1981-01-02   0
1981-01-03   0
1981-01-04   1
1981-01-05   0
1981-01-06   1
1981-01-07   1
1981-01-08   1
1981-01-09   0
1981-01-10   0
1981-01-11   1
1981-01-12   1
1981-01-13   1
1981-01-14   1
1981-01-15   1
1981-01-16   0
..........   .
Now I have to calculate the number of consecutive wet days for each year when at least 3 consecutive days received rainfall and the longest consecutive days of rainfall in a year. If 3 or more than 3 consecutive days (any number) received rainfall I will consider it as a single event.
My output will be like this
Year      No of consecutive wet-days   longest consecutive wet-days
1981      2                            5
.
.
How can we do this in R? If I can solve for a station, I can iterate for all stations in R.
Thank you in advance for your help :)
Another possible solution (I thank @DarrenTsai for his comments, which have improved this solution):
library(tidyverse)
library(lubridate)
df %>% 
  group_by(Year = year(ymd(Date))) %>%
  mutate(x = list(rle(Rainfall))) %>% 
  summarise(ncons = sum(x[[1]]$lengths >= 3 & x[[1]]$values == 1),
            longest = ifelse(sum(x[[1]]$values == 1) == 0, 0, 
                max(x[[1]]$lengths[x[[1]]$values == 1])))
#> # A tibble: 2 × 3
#>    Year ncons longest
#>   <dbl> <int>   <int>
#> 1  1981     2       5
#> 2  1982     2       4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With