Identifying the duration and rainfall depth of precipitation events

This is a follow-up to my previous post Identifying and Determining the Average Rainfall Intensity of Precipitation Events and Associated Increases in Streamflow - General - Posit Community.

I have a .csv file (shown below) containing precipitation and discharge data taken at 15-minute intervals for 12 years.

Date, Time, Precipitation, Discharge
2010-05-13,10:30:00,0,0.224
2010-05-13,10:45:00,0,0.225
2010-05-13,11:00:00,0,0.225

I want use the precipitation data to identify the rainfall depth and duration of precipitation events based on the cumulative total of precipitation values with less than 4 hours of zero precipitation values in between. For example, precipitation began at 2010-05-13 11:30:00 and stopped 2010-05-13 21:00:00. Precipitation then began again at 2010-05-13 22:45:00 and continued until 2010-05-14 00:15:00. Because there is less than 4 hours between the time that the precipitation stopped (2010-05-13 21:00:00) and the time that the precipitation began (2010-05-13 22:45:00), these should be treated as a single event. After precipitation events have been identified, I want to determine the duration of the event. For example, considering the data provided above, between 2010-05-13 11:30:00 and 2010-05-14 00:15:00 (my rainfall event previously described above), there would be an event duration of 12 hr and 45 min (12.75 hr).

Based on this I want to create a new data object with the following format:

Start_Date_Time, End_Date_Time, Event_Duration, Total_Precipitation, Avg_Precipitation_Intensity

In this data object, "Start_Date_Time" is the date and time that the rainfall began, "End_Date_Time" is the date and time when the rainfall stopped for at least 4 hours, and "Event_Duration" is the number of hours that have elapsed between "Start_Date_Time" and "End_Date_Time". "Total_Precipitation" is the total precipitation that has fallen between Start_Date_Time and End_Date_Time and "Avg_Precipitation_Intensity" is calculated by dividing "Total_Precipitation" by "Event_Duration". Does anyone have any advice as to how achieve this?

TIA

1 Like

Hi, it would help if you provided a reproducible example as well as some code that you have tried. This is also asked for in your other post.

Thank you for the suggestion. Below is a reproducible example for the first 11 days in my dataset. I haven't tried any code yet as I have no idea where to begin. I am hoping that somebody could provide some advice on how to start.

structure(list(Date = c("2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", "2010-05-01", 
"2010-05-01", "2010-05-01", "2010-05-01", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", 
"2010-05-02", "2010-05-02", "2010-05-02", "2010-05-02", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", "2010-05-03", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", "2010-05-04", 
"2010-05-04", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", "2010-05-05", 
"2010-05-05", "2010-05-05", "2010-05-06", "2010-05-06", "2010-05-06", 
"2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", 
"2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", 
"2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", "2010-05-06", 
"2010-05-06", "2010-05-06"), Time = c("0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00", "5:00:00", 
"5:15:00", "5:30:00", "5:45:00", "6:00:00", "6:15:00", "6:30:00", 
"6:45:00", "7:00:00", "7:15:00", "7:30:00", "7:45:00", "8:00:00", 
"8:15:00", "8:30:00", "8:45:00", "9:00:00", "9:15:00", "9:30:00", 
"9:45:00", "10:00:00", "10:15:00", "10:30:00", "10:45:00", "11:00:00", 
"11:15:00", "11:30:00", "11:45:00", "12:00:00", "12:15:00", "12:30:00", 
"12:45:00", "13:00:00", "13:15:00", "13:30:00", "13:45:00", "14:00:00", 
"14:15:00", "14:30:00", "14:45:00", "15:00:00", "15:15:00", "15:30:00", 
"15:45:00", "16:00:00", "16:15:00", "16:30:00", "16:45:00", "17:00:00", 
"17:15:00", "17:30:00", "17:45:00", "18:00:00", "18:15:00", "18:30:00", 
"18:45:00", "19:00:00", "19:15:00", "19:30:00", "19:45:00", "20:00:00", 
"20:15:00", "20:30:00", "20:45:00", "21:00:00", "21:15:00", "21:30:00", 
"21:45:00", "22:00:00", "22:15:00", "22:30:00", "22:45:00", "23:00:00", 
"23:15:00", "23:30:00", "23:45:00", "0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00", "5:00:00", 
"5:15:00", "5:30:00", "5:45:00", "6:00:00", "6:15:00", "6:30:00", 
"6:45:00", "7:00:00", "7:15:00", "7:30:00", "7:45:00", "8:00:00", 
"8:15:00", "8:30:00", "8:45:00", "9:00:00", "9:15:00", "9:30:00", 
"9:45:00", "10:00:00", "10:15:00", "10:30:00", "10:45:00", "11:00:00", 
"11:15:00", "11:30:00", "11:45:00", "12:00:00", "12:15:00", "12:30:00", 
"12:45:00", "13:00:00", "13:15:00", "13:30:00", "13:45:00", "14:00:00", 
"14:15:00", "14:30:00", "14:45:00", "15:00:00", "15:15:00", "15:30:00", 
"15:45:00", "16:00:00", "16:15:00", "16:30:00", "16:45:00", "17:00:00", 
"17:15:00", "17:30:00", "17:45:00", "18:00:00", "18:15:00", "18:30:00", 
"18:45:00", "19:00:00", "19:15:00", "19:30:00", "19:45:00", "20:00:00", 
"20:15:00", "20:30:00", "20:45:00", "21:00:00", "21:15:00", "21:30:00", 
"21:45:00", "22:00:00", "22:15:00", "22:30:00", "22:45:00", "23:00:00", 
"23:15:00", "23:30:00", "23:45:00", "0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00", "5:00:00", 
"5:15:00", "5:30:00", "5:45:00", "6:00:00", "6:15:00", "6:30:00", 
"6:45:00", "7:00:00", "7:15:00", "7:30:00", "7:45:00", "8:00:00", 
"8:15:00", "8:30:00", "8:45:00", "9:00:00", "9:15:00", "9:30:00", 
"9:45:00", "10:00:00", "10:15:00", "10:30:00", "10:45:00", "11:00:00", 
"11:15:00", "11:30:00", "11:45:00", "12:00:00", "12:15:00", "12:30:00", 
"12:45:00", "13:00:00", "13:15:00", "13:30:00", "13:45:00", "14:00:00", 
"14:15:00", "14:30:00", "14:45:00", "15:00:00", "15:15:00", "15:30:00", 
"15:45:00", "16:00:00", "16:15:00", "16:30:00", "16:45:00", "17:00:00", 
"17:15:00", "17:30:00", "17:45:00", "18:00:00", "18:15:00", "18:30:00", 
"18:45:00", "19:00:00", "19:15:00", "19:30:00", "19:45:00", "20:00:00", 
"20:15:00", "20:30:00", "20:45:00", "21:00:00", "21:15:00", "21:30:00", 
"21:45:00", "22:00:00", "22:15:00", "22:30:00", "22:45:00", "23:00:00", 
"23:15:00", "23:30:00", "23:45:00", "0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00", "5:00:00", 
"5:15:00", "5:30:00", "5:45:00", "6:00:00", "6:15:00", "6:30:00", 
"6:45:00", "7:00:00", "7:15:00", "7:30:00", "7:45:00", "8:00:00", 
"8:15:00", "8:30:00", "8:45:00", "9:00:00", "9:15:00", "9:30:00", 
"9:45:00", "10:00:00", "10:15:00", "10:30:00", "10:45:00", "11:00:00", 
"11:15:00", "11:30:00", "11:45:00", "12:00:00", "12:15:00", "12:30:00", 
"12:45:00", "13:00:00", "13:15:00", "13:30:00", "13:45:00", "14:00:00", 
"14:15:00", "14:30:00", "14:45:00", "15:00:00", "15:15:00", "15:30:00", 
"15:45:00", "16:00:00", "16:15:00", "16:30:00", "16:45:00", "17:00:00", 
"17:15:00", "17:30:00", "17:45:00", "18:00:00", "18:15:00", "18:30:00", 
"18:45:00", "19:00:00", "19:15:00", "19:30:00", "19:45:00", "20:00:00", 
"20:15:00", "20:30:00", "20:45:00", "21:00:00", "21:15:00", "21:30:00", 
"21:45:00", "22:00:00", "22:15:00", "22:30:00", "22:45:00", "23:00:00", 
"23:15:00", "23:30:00", "23:45:00", "0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00", "5:00:00", 
"5:15:00", "5:30:00", "5:45:00", "6:00:00", "6:15:00", "6:30:00", 
"6:45:00", "7:00:00", "7:15:00", "7:30:00", "7:45:00", "8:00:00", 
"8:15:00", "8:30:00", "8:45:00", "9:00:00", "9:15:00", "9:30:00", 
"9:45:00", "10:00:00", "10:15:00", "10:30:00", "10:45:00", "11:00:00", 
"11:15:00", "11:30:00", "11:45:00", "12:00:00", "12:15:00", "12:30:00", 
"12:45:00", "13:00:00", "13:15:00", "13:30:00", "13:45:00", "14:00:00", 
"14:15:00", "14:30:00", "14:45:00", "15:00:00", "15:15:00", "15:30:00", 
"15:45:00", "16:00:00", "16:15:00", "16:30:00", "16:45:00", "17:00:00", 
"17:15:00", "17:30:00", "17:45:00", "18:00:00", "18:15:00", "18:30:00", 
"18:45:00", "19:00:00", "19:15:00", "19:30:00", "19:45:00", "20:00:00", 
"20:15:00", "20:30:00", "20:45:00", "21:00:00", "21:15:00", "21:30:00", 
"21:45:00", "22:00:00", "22:15:00", "22:30:00", "22:45:00", "23:00:00", 
"23:15:00", "23:30:00", "23:45:00", "0:00:00", "0:15:00", "0:30:00", 
"0:45:00", "1:00:00", "1:15:00", "1:30:00", "1:45:00", "2:00:00", 
"2:15:00", "2:30:00", "2:45:00", "3:00:00", "3:15:00", "3:30:00", 
"3:45:00", "4:00:00", "4:15:00", "4:30:00", "4:45:00"), Precipitation = c(0, 
0, 0, 0.2, 0.2, 0, 0, 0.2, 0.4, 0, 0, 0.2, 0, 0, 0, 0, 0.2, 0, 
0, 0.6, 0.4, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.2, 0, 0.2, 0.6, 1, 0.8, 0.8, 0.2, 0, 0, 0.2, 
0, 0, 0, 0, 1, 0.2, 0.4, 1, 0.2, 0.2, 0.2, 0.2, 0.2, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4, 2.4, 2.2, 1, 0.4, 0, 0, 
0, 0, 0, 0, 0, 0.4, 0, 0.2, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Discharge = c(0.299, 
0.302, 0.305, 0.308, 0.312, 0.317, 0.321, 0.326, 0.33, 0.339, 
0.352, 0.38, 0.41, 0.428, 0.424, 0.419, 0.415, 0.41, 0.411, 0.412, 
0.414, 0.415, 0.416, 0.44, 0.459, 0.465, 0.495, 0.495, 0.495, 
0.495, 0.495, 0.495, 0.471, 0.453, 0.44, 0.431, 0.422, 0.423, 
0.423, 0.424, 0.424, 0.425, 0.425, 0.426, 0.427, 0.427, 0.428, 
0.428, 0.429, 0.429, 0.43, 0.43, 0.431, 0.431, 0.432, 0.432, 
0.433, 0.433, 0.434, 0.434, 0.435, 0.441, 0.471, 0.527, 0.565, 
0.562, 0.559, 0.556, 0.552, 0.665, 0.892, 0.941, 0.937, 0.933, 
0.928, 0.924, 0.843, 0.812, 0.765, 0.729, 0.693, 0.672, 0.644, 
0.617, 0.611, 0.591, 0.578, 0.569, 0.559, 0.55, 0.54, 0.538, 
0.536, 0.534, 0.532, 0.53, 0.528, 0.526, 0.524, 0.521, 0.52, 
0.518, 0.516, 0.515, 0.513, 0.511, 0.51, 0.508, 0.506, 0.504, 
0.503, 0.497, 0.49, 0.484, 0.483, 0.482, 0.481, 0.48, 0.478, 
0.477, 0.476, 0.475, 0.474, 0.472, 0.471, 0.47, 0.469, 0.468, 
0.466, 0.465, 0.464, 0.463, 0.462, 0.461, 0.46, 0.459, 0.458, 
0.457, 0.456, 0.455, 0.454, 0.453, 0.452, 0.451, 0.45, 0.449, 
0.448, 0.448, 0.447, 0.447, 0.446, 0.446, 0.445, 0.445, 0.444, 
0.444, 0.443, 0.443, 0.442, 0.442, 0.441, 0.441, 0.44, 0.44, 
0.439, 0.439, 0.438, 0.438, 0.438, 0.437, 0.437, 0.436, 0.436, 
0.435, 0.435, 0.434, 0.434, 0.433, 0.433, 0.432, 0.432, 0.431, 
0.431, 0.43, 0.429, 0.428, 0.427, 0.426, 0.425, 0.425, 0.424, 
0.423, 0.422, 0.421, 0.42, 0.419, 0.418, 0.417, 0.417, 0.416, 
0.415, 0.414, 0.413, 0.411, 0.408, 0.405, 0.403, 0.4, 0.398, 
0.395, 0.395, 0.394, 0.393, 0.393, 0.392, 0.391, 0.391, 0.39, 
0.39, 0.389, 0.388, 0.388, 0.387, 0.386, 0.386, 0.385, 0.385, 
0.384, 0.383, 0.383, 0.382, 0.381, 0.381, 0.38, 0.38, 0.379, 
0.378, 0.378, 0.377, 0.376, 0.375, 0.374, 0.373, 0.372, 0.371, 
0.37, 0.368, 0.367, 0.366, 0.365, 0.364, 0.364, 0.364, 0.364, 
0.364, 0.363, 0.363, 0.363, 0.363, 0.363, 0.363, 0.362, 0.362, 
0.362, 0.362, 0.362, 0.361, 0.361, 0.361, 0.361, 0.361, 0.36, 
0.36, 0.36, 0.36, 0.36, 0.36, 0.359, 0.359, 0.359, 0.359, 0.359, 
0.358, 0.358, 0.358, 0.358, 0.358, 0.357, 0.357, 0.357, 0.357, 
0.357, 0.357, 0.356, 0.356, 0.356, 0.356, 0.356, 0.355, 0.355, 
0.355, 0.355, 0.355, 0.355, 0.354, 0.354, 0.354, 0.354, 0.354, 
0.353, 0.353, 0.353, 0.353, 0.353, 0.352, 0.352, 0.352, 0.352, 
0.352, 0.351, 0.351, 0.35, 0.35, 0.349, 0.349, 0.348, 0.348, 
0.347, 0.347, 0.346, 0.346, 0.345, 0.345, 0.344, 0.344, 0.343, 
0.343, 0.342, 0.342, 0.341, 0.341, 0.34, 0.34, 0.34, 0.339, 0.339, 
0.339, 0.339, 0.339, 0.34, 0.34, 0.34, 0.34, 0.341, 0.341, 0.341, 
0.341, 0.342, 0.342, 0.342, 0.342, 0.343, 0.343, 0.343, 0.344, 
0.344, 0.344, 0.344, 0.345, 0.345, 0.345, 0.345, 0.346, 0.346, 
0.346, 0.346, 0.347, 0.347, 0.347, 0.347, 0.348, 0.348, 0.352, 
0.366, 0.386, 0.434, 0.501, 0.533, 0.577, 0.629, 0.627, 0.625, 
0.623, 0.597, 0.571, 0.527, 0.533, 0.584, 0.623, 0.728, 0.795, 
0.899, 0.932, 0.907, 0.875, 0.827, 0.743, 0.685, 0.637, 0.626, 
0.617, 0.61, 0.604, 0.597, 0.597, 0.598, 0.598, 0.598, 0.599, 
0.599, 0.599, 0.599, 0.6, 0.6, 0.6, 0.601, 0.601, 0.601, 0.601, 
0.602, 0.602, 0.602, 0.602, 0.603, 0.603, 0.603, 0.604, 0.604, 
0.604, 0.714, 0.781, 1.095, 1.095, 1.249, 1.381, 1.316, 1.316, 
1.25, 1.145, 1.042, 0.965, 0.916, 0.815, 0.817, 0.82, 0.851, 
0.851, 0.85, 0.849, 0.849, 0.848, 0.848, 0.847, 0.846, 0.846, 
0.845, 0.845, 0.844, 0.843, 0.843, 0.842, 0.842, 0.841, 0.84, 
0.84, 0.839, 0.839, 0.838, 0.837, 0.837, 0.836, 0.836, 0.835, 
0.834, 0.834, 0.833, 0.833, 0.832, 0.831, 0.831, 0.83, 0.83, 
0.829, 0.829, 0.827, 0.826, 0.825, 0.824, 0.822, 0.821)), row.names = c(NA, 
500L), class = "data.frame")

You should check the calculations. It is just me playing around.

library(tidyverse)
library(lubridate)
library(glue)

as_tibble(df) %>% 
  mutate(Start_Date_Time = ymd_hms(glue("{Date}_{Time}")),
         End_Date_Time = lag(Start_Date_Time),
         no_rain = Precipitation == 0) %>% 
  group_by(no_rain) %>% 
  mutate(counter = seq_along(no_rain),
         counter = if_else(!no_rain, 0, counter))

df1 <- as_tibble(df) %>% 
  mutate(End_Date_Time = ymd_hms(glue("{Date}_{Time}")),
         Start_Date_Time = lag(End_Date_Time),
         no_rain = Precipitation == 0) %>% 
  mutate(Start_Date_Time = if_else(is.na(Start_Date_Time), End_Date_Time - minutes(15), Start_Date_Time)) %>% 
  # for precipitation
  group_by(grp = with(rle(no_rain), rep(seq_along(lengths), lengths))) %>% 
  mutate(no_rain_count = seq_along(grp),
         no_rain_count = if_else(!no_rain, 0, no_rain_count)) %>% 
  ungroup() %>% 
  select(Date:Discharge, Start_Date_Time, End_Date_Time:no_rain_count)


# where rain had stopped for more than four hours
df2 <- df1 %>% 
  group_by(grp) %>% 
  filter(no_rain_count >= 16,
         no_rain_count == max(no_rain_count))


# the groups preceeding the no rain periods
df3 <-  reduce(list(
  # start times
  df1 %>% 
    filter(grp %in% (df2$grp - 1)) %>% 
    group_by(grp) %>% 
    filter(Start_Date_Time == min(Start_Date_Time)) %>% 
    select(grp, Start_Date_Time),
  # end times
  df1 %>% 
    filter(grp %in% (df2$grp - 1)) %>% 
    group_by(grp) %>% 
    filter(End_Date_Time == max(End_Date_Time)) %>% 
    select(grp, End_Date_Time),
  # precipitation
  df1 %>% 
    filter(grp %in% (df2$grp - 1)) %>% 
    group_by(grp) %>% 
    summarise(Total_Precipitation = sum(Precipitation))
),
left_join,
by = "grp") %>% 
  ungroup()


# final table
df3 %>% 
  mutate(Event_Duration = interval(Start_Date_Time, End_Date_Time) / dhours(1),
         Avg_Precipitation_Intensity = Total_Precipitation / Event_Duration)

# A tibble: 4 x 6
    grp Start_Date_Time     End_Date_Time       Total_Precipitation Event_Duration Avg_Precipitation_Intensity
  <int> <dttm>              <dttm>                            <dbl>          <dbl>                       <dbl>
1    12 2010-05-01 07:15:00 2010-05-01 07:30:00                 0.2           0.25                         0.8
2    14 2010-05-02 00:15:00 2010-05-02 00:30:00                 3             0.25                        12  
3    22 2010-05-05 02:00:00 2010-05-05 04:15:00                 3.6           2.25                         1.6
4    28 2010-05-05 16:30:00 2010-05-05 17:00:00                 0.4           0.5                          0.8


Thank you, when I run the as_tibble() function, I get the error "Error in as.data.frame.default(value, stringsAsFactors = FALSE) : cannot coerce class ā€˜"function"ā€™ to a data.frame". Does anyone know how to fix this error?

> # Determine event duration for each precipitation event 
> as_tibble(df) %>% 
+   mutate(Start_Date_Time = ymd_hms(glue("{Date}_{Time}")),
+          End_Date_Time = lag(Start_Date_Time),
+          no_rain = Precipitation == 0) %>% 
+   group_by(no_rain) %>% 
+   mutate(counter = seq_along(no_rain),
+          counter = if_else(!no_rain, 0, counter))
Error in as.data.frame.default(value, stringsAsFactors = FALSE) : 
  cannot coerce class ā€˜"function"ā€™ to a data.frame```

I've called your dataset df. Yours might have a different name. Just change it.

Otherwise, what is your dataset called? What happens if you type str(df) but replace df with whatever your dataset is.

I called my dataset data.dat. Below is what happens when I type str(data.dat):

> str(data.dat)
'data.frame':	227455 obs. of  4 variables:
 $ Date         : chr  "2010-05-01" "2010-05-01" "2010-05-01" "2010-05-01" ...
 $ Time         : chr  "0:00:00" "0:15:00" "0:30:00" "0:45:00" ...
 $ Precipitation: num  0 0 0 0.2 0.2 0 0 0.2 0.4 0 ...
 $ Discharge    : num  0.299 0.302 0.305 0.308 0.312 0.317 0.321 0.326 0.33 0.339 ...```

I changed where you had named the dataset df to my name "data.dat" in the as_tibble() command and now receive the warning

Warning message:
There was 1 warning in `mutate()`.
ā„¹ In argument: `Start_Date_Time = ymd_hms(glue("{Date}_{Time}"))`.
Caused by warning:
!  1 failed to parse. 

My tibble table now looks like this:

   Date       Time   Precipitation Discharge Start_Date_Time     End_Date_Time       no_rain counter
   <chr>      <chr>          <dbl>     <dbl> <dttm>              <dttm>              <lgl>     <dbl>
 1 2010-05-01 0:00:ā€¦           0       0.299 2010-05-01 00:00:00 NA                  TRUE          1
 2 2010-05-01 0:15:ā€¦           0       0.302 2010-05-01 00:15:00 2010-05-01 00:00:00 TRUE          2
 3 2010-05-01 0:30:ā€¦           0       0.305 2010-05-01 00:30:00 2010-05-01 00:15:00 TRUE          3
 4 2010-05-01 0:45:ā€¦           0.2     0.308 2010-05-01 00:45:00 2010-05-01 00:30:00 FALSE         0
 5 2010-05-01 1:00:ā€¦           0.2     0.312 2010-05-01 01:00:00 2010-05-01 00:45:00 FALSE         0
 6 2010-05-01 1:15:ā€¦           0       0.317 2010-05-01 01:15:00 2010-05-01 01:00:00 TRUE          4
 7 2010-05-01 1:30:ā€¦           0       0.321 2010-05-01 01:30:00 2010-05-01 01:15:00 TRUE          5
 8 2010-05-01 1:45:ā€¦           0.2     0.326 2010-05-01 01:45:00 2010-05-01 01:30:00 FALSE         0
 9 2010-05-01 2:00:ā€¦           0.4     0.33  2010-05-01 02:00:00 2010-05-01 01:45:00 FALSE         0
10 2010-05-01 2:15:ā€¦           0       0.339 2010-05-01 02:15:00 2010-05-01 02:00:00 TRUE          6```

Maybe one of the times was in the wrong format? But otherwise all the rest of them should be fine.

The counter might not be working. You might be able to use something like this. The idea is to count the periods where there is no rain, then just grab the highest numbers, as long as the period was >= 16.

Thank you, I think that I figured it out. I had a null first value, causing it to fail to parse. When I ran the na.omit() command the as.tibble() command worked. However, now when I run the code:

df1 <- as_tibble(df) %>% 
  mutate(End_Date_Time = ymd_hms(glue("{Date}_{Time}")),
         Start_Date_Time = lag(End_Date_Time),
         no_rain = Precipitation == 0) %>% 
  mutate(Start_Date_Time = if_else(is.na(Start_Date_Time), End_Date_Time - minutes(15), Start_Date_Time)) %>% 

I get the error

Error in as.data.frame.default(value, stringsAsFactors = FALSE) : 
  cannot coerce class ā€˜"function"ā€™ to a data.frame

Does anyone know how to get around this error?

Are you just running the section in your last post or are you also running the bit after the # for precipitation comment? I can't really think of what the issue is at the moment.

Actually, does df exist, or should it be data.dat?

Here is an approach that uses the table rain_events from the post Identifying and Determining the Average Rainfall Intensity of Precipitation Events and Associated Increases in Streamflow Part 1 - #4 by dromano

rain_events |> 
  mutate(
    datetime = str_c(Date, Time) |> parse_date_time('YmdHMS'),
    .after = Time
    ) |> 
  filter(!in_dry_spell) |> 
  group_by(rain_event) |> 
  mutate(
    start = first(datetime),
    end = last(datetime), 
    num_observations = n(),
    hours =  15 * num_observations / 60,
    total_precip = sum(Precipitation),
    avg_precip_rate = total_precip / hours
  ) |> 
  ungroup() |> 
  select(contains(c('rain','start', 'end', 'hour', 'total', 'avg'))) |> 
  distinct()

[Edit: In the mutate() section, changed "begin = " to "start =" .]

Thank you @dromano that is great. I ran your code which provided me with the following tibble

   rain_event end                 hours total_precip avg_precip_rate
        <dbl> <dttm>              <dbl>        <dbl>           <dbl>
 1          1 2010-05-01 07:30:00  7             2.8            0.4 
 2          2 2010-05-02 00:30:00  0.25          3             12   
 3          3 2010-05-05 04:15:00  6             7.6            1.27
 4          4 2010-05-05 17:00:00  4             7.2            1.8 
 5          5 2010-05-14 02:45:00 15.5          17.8            1.15
 6          6 2010-05-14 17:45:00  2.5           0.4            0.16
 7          7 2010-05-16 08:15:00  0.25          0.2            0.8 
 8          8 2010-05-30 18:45:00  1.25          2.4            1.92
 9          9 2010-05-31 22:30:00  0.25          0.2            0.8 
10         10 2010-06-01 03:00:00  0.25          0.2            0.8 

I would now like to refer back to my original .csv file

Date, Time, Precipitation, Discharge
2010-05-13,10:30:00,0,0.224
2010-05-13,10:45:00,0,0.225
2010-05-13,11:00:00,0,0.225

For each precipitation event (i.e., "rain_event") I would like to determine the discharge rate right before the event occurred, and the maximum discharge rate of the event. I would then like to create a tibble table with the format "date", "hours", "total_precip", avg_precip_rate", "pre_event_discharge", "max_event_discharge", "increase_discharge" as shown below:

rain_event, start_date, hours, total_precip, avg_precip_rate, pre_event_discharge, max_event_discharge, increase_discharge

In this object:

  • rain_event is the event number
  • start_date is the date that the rainfall event started
  • hours is the number of hours that the rainfall lasted (as a decimal number)
  • total_precip is the total amount of precipitation that had fallen (as previously calculated)
  • avg_precip_rate the "total_precip" divided by "hours"
  • pre-event discharge is the discharge rate before the rainfall occurred
  • max_event_discharge is the maximum discharge rate from the time that the event started to four hours after the event
  • increase_discharge is the difference between the pre_event_discharge and the max_event_discharge

Does anyone have any suggestions for creating this data object?

I see I missed a typo: in the mutate() section, begin = should be start =. I'll make the edit now.

What does this mean?

Also, this sounds like a new task, so should probably be in a new topic.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.