

# Add an id to each event (rain or not), to group by on the pivot table # If within the first six rows there is no rain registered, then the event flag should change to 0 # Correct for the case when the dataset starts with no rain for less than six consecutive times

# when it is 0 but not for six consecutive times, it is still a rain event # Set a flag for an event happening, when there is rain there is a rain event,

Mutate(rainlength = rep(rle(rainflag)$lengths, rle(rainflag)$lengths)) %>% # Use `rle`` which indicates how many times consecutive values happen, and `rep`` to repeat it for each row. # Create a column that contains the number of consecutive times there was rain or not. Mutate(rainflag = ifelse(Precip_in > 0, 1, 0)) %>% # Set a rain flag if there is rain registered on the gauge # Set data column as POSIXct, important for calculating duration afterwardsĭata % mutate(DateTime = as.POSIXct(DateTime, format = '%m/%d/%Y %H:%M')) I saved the data in a variable called data. If someone is still looking for a way to solve this question, here is my 'tidy' approach on it. Finally, I'd like to get an output (similar to a pivot table) that sums the total precip in inches of each unique event, and also gives me the start and stop time, and total duration of event.Įvent ID Precip (in) Event STart Event Stop Time (hours)ĭF1$event = 2 & DF1$event = 1, "flag", "NA") Then if it does, it should create a flag. I'm having trouble to get this unique flag part, because I need R to "look ahead" in the precip column to see if it rains within 6 hours in the future.

The event flag or ID could be the start timedate stamp of the event or just a n+1 the last identifier (1,1,1,1,2,2,2,2) etc. My plan of attack was to create a column that would contain a unique "flags" for the events. I want to start with an inter event period of 6 dry hours between events. identify unique rainfall "events" in the dataset. Here is what I am trying to make the code do:
