I have a multi-annual temperature time-series with a datetime column (with irregular time steps) as such :
daytime Temperature
<dttm> <dbl>
1981-10-01 09:00:00 22
1981-10-02 09:00:00 21.6
1981-10-03 09:00:00 20.3
1981-10-04 09:00:00 20.4
1981-10-05 09:00:00 20.6
1981-10-05 11:00:00 21
I would like to find all cases that match 2 conditions where temperature difference (max-min) within a certain time span (48h) is at least 4°C. Put in other words, find all the rapid and important temperature changes (wether increases or decreases. Ideally the outcome would be a dataframe with 1 column containing the first date of each detected case and a second column with the asssociated temperature difference (>= 4°C) of the given case.
I tried through the Rbeast tools which detects breakpoints but it doesn't seem to be able to detect such short-term variations.
Thank you very much you smart people !!!
I'm going to create my own sample data, since the provided data will never trigger the 4-degree difference. I'll include a gap so that we show it's a rolling 48h window and not a rolling 2-row window.
(I'm inferring you are using
dplyrsince you have atbl_dfsample data.)Note that since we don't have
10-04or10-05, the gap from14.4to19.4should not trigger anything.From here, we need a rolling-window to define the "48 hours" thing.
With that, filtering out those above 4 degrees is straight-forward:
The first step above is to define
width, which indicates how many rows (including the current row) are included in the "next 48 hours"; you can see that it reduces when it approaches the 2-day gap I imposed in the sample data. The day prior to the 2-day gap we have atempdiffof 0, which is because14.4is the only observation available, anddiff(range(14.4))is 0.