Calculate start time of an event by working backwards from the end time of an event.

Hi, this is a follow-up to my previous post Identifying the duration and rainfall depth of precipitation events - General - Posit Community.

In the previous forum, I identified rainfall events and created the following tibble table showing these rainfall events. In this table "rain_event" is just an identifier of the event number, i.e., the first event is 1, second event is 2, etc. "rain_event_end" is the date and time that the precipitation event ended, "hours" is the number of hours that had elapsed between the time the precipitation event started and the time the event ended, "total_precip" is the total volume of precipitation that had fallen over the rainfall event, and "avg_precip_rate" is the average precipitation rate (i.e., total_precip divided by hours).

    rain_event                     end       hours        total_precip      avg_precip_rate
        <dbl>                   <dttm>       <dbl>               <dbl>           <dbl>
 1          1      2010-05-01 07:30:00       7.00                  2.8            0.4 
 2          2      2010-05-02 00:30:00       0.25                  3.0            12   
 3          3      2010-05-05 04:15:00       6.00                  7.6            1.27
 4          4      2010-05-05 17:00:00       4.00                  7.2            1.8 
 5          5      2010-05-14 02:45:00      15.50                 17.8            1.15
 6          6      2010-05-14 17:45:00       2.50                  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 `````

Using the end time of each event (end) and the duration of the event (hours) want to create another column in this tibble table for the start date and time of the event. For example, for rain_event 1, there was an end time of 2010-05-01 7:30:00 and a duration of 7 hours. This would indicate that the start time of the event was 00:00:30 (i.e., 7 hours before the event). I would like to calculate the start date and time for each event and put the data in a new table with the format

rain_event      start     end     hours     total_precip     avg_precip_rate
     <dbl>     <dttm>  <dttm>     <dbl>            <dbl>               <dbl>

Does anyone have any advice as to how to achieve this? TIA

Hi @brant ,

you can use the lubridate package for this.

So if your data frame is df you would do:

df$start <- df$end - hours(df$hours)

hours is a function from the package.

Thanks, am I able to add the resultant start time as a column into my original table?

My answer does exactly that.

Hi Brant,

The start time is calculated in the code posted here — the original code had a typo.

This answer is not correct for the structure of the data used by the poster, who is keeping track of precipitation in 15-minute intervals. So if an event starts at time 1 and ends at time 3, it's actually 45 minutes long.

Hi @dromano,

this was not the way it was posted in the question.

Yeah, the code will not work as is but

df$start <- df$end - duration(num = df$hours, units = "hours")

answers the question with the data and explanation given.

1 Like

This not correct: From the post you linked to, the start time is the 15-minute interval when precipitation is first detected and the duration of the event includes this and (simplifying for the sake of clarity) all the subsequent observations where precipitation occurs before an extended period of no precipitation. So if, say, rain was first detected in observation 1 (at 00:00:00), and then in 2 (at 00:00:15) and 3 (at 00:0030), but not in observation 4 or later, the duration is 45 minutes, but the start time is not 45 minutes before 00:00:30.