Wählen Sie Niederschlagsereignisse aus und berechnen Sie das Niederschlagsereignis insgesamt aus Zeitreihendaten
library(dplyr)
# Set data column as POSIXct, important for calculating duration afterwards
data <- data %>% mutate(DateTime = as.POSIXct(DateTime, format = '%m/%d/%Y %H:%M'))
flags <- data %>%
# Set a rain flag if there is rain registered on the gauge
mutate(rainflag = ifelse(Precip_in > 0, 1, 0)) %>%
# Create a column that contains the number of consecutive times there was rain or not.
# Use `rle`` which indicates how many times consecutive values happen, and `rep`` to repeat it for each row.
mutate(rainlength = rep(rle(rainflag)$lengths, rle(rainflag)$lengths)) %>%
# Set a flag for an event happening, when there is rain there is a rain event,
# when it is 0 but not for six consecutive times, it is still a rain event
mutate(
eventflag = ifelse(
rainflag == 1,
1,
ifelse(
rainflag == 0 & rainlength < 6,
1,
0
)
)
) %>%
# Correct for the case when the dataset starts with no rain for less than six consecutive times
# If within the first six rows there is no rain registered, then the event flag should change to 0
mutate(eventflag = ifelse(row_number() < 6 & rainflag == 0, 0, eventflag)) %>%
# Add an id to each event (rain or not), to group by on the pivot table
mutate(eventid = rep(seq(1,length(rle(eventflag)$lengths)), rle(eventflag)$lengths))
rain_pivot <- flags %>%
# Select only the rain events
filter(eventflag == 1) %>%
# Group by id
group_by(eventid) %>%
summarize(
precipitation = sum(Precip_in),
eventStart = first(DateTime),
eventEnd = last(DateTime)
) %>%
# Compute time difference as duration of event, add 1 hour, knowing that the timestamp is the time when the rain record ends
mutate(time = as.numeric(difftime(eventEnd,eventStart, units = 'h')) + 1)
rain_pivot
#> # A tibble: 2 x 5
#> eventid precipitation eventStart eventEnd time
#> <int> <dbl> <dttm> <dttm> <dbl>
#> 1 2 0.07 2017-10-06 17:00:00 2017-10-06 22:00:00 6
#> 2 4 0.01 2017-10-07 15:00:00 2017-10-07 15:00:00 1
Lazy Lizard