1library(dplyr)
2
3# Set data column as POSIXct, important for calculating duration afterwards
4data <- data %>% mutate(DateTime = as.POSIXct(DateTime, format = '%m/%d/%Y %H:%M'))
5
6flags <- data %>%
7 # Set a rain flag if there is rain registered on the gauge
8 mutate(rainflag = ifelse(Precip_in > 0, 1, 0)) %>%
9 # Create a column that contains the number of consecutive times there was rain or not.
10 # Use `rle`` which indicates how many times consecutive values happen, and `rep`` to repeat it for each row.
11 mutate(rainlength = rep(rle(rainflag)$lengths, rle(rainflag)$lengths)) %>%
12 # Set a flag for an event happening, when there is rain there is a rain event,
13 # when it is 0 but not for six consecutive times, it is still a rain event
14 mutate(
15 eventflag = ifelse(
16 rainflag == 1,
17 1,
18 ifelse(
19 rainflag == 0 & rainlength < 6,
20 1,
21 0
22 )
23 )
24 ) %>%
25 # Correct for the case when the dataset starts with no rain for less than six consecutive times
26 # If within the first six rows there is no rain registered, then the event flag should change to 0
27 mutate(eventflag = ifelse(row_number() < 6 & rainflag == 0, 0, eventflag)) %>%
28 # Add an id to each event (rain or not), to group by on the pivot table
29 mutate(eventid = rep(seq(1,length(rle(eventflag)$lengths)), rle(eventflag)$lengths))
30
31rain_pivot <- flags %>%
32 # Select only the rain events
33 filter(eventflag == 1) %>%
34 # Group by id
35 group_by(eventid) %>%
36 summarize(
37 precipitation = sum(Precip_in),
38 eventStart = first(DateTime),
39 eventEnd = last(DateTime)
40 ) %>%
41 # Compute time difference as duration of event, add 1 hour, knowing that the timestamp is the time when the rain record ends
42 mutate(time = as.numeric(difftime(eventEnd,eventStart, units = 'h')) + 1)
43
44rain_pivot
45#> # A tibble: 2 x 5
46#> eventid precipitation eventStart eventEnd time
47#> <int> <dbl> <dttm> <dttm> <dbl>
48#> 1 2 0.07 2017-10-06 17:00:00 2017-10-06 22:00:00 6
49#> 2 4 0.01 2017-10-07 15:00:00 2017-10-07 15:00:00 1
50