Search code examples
rmoving-averagehighest

How can I find the maximal increase over 6 values in a dataset


I have a data set of core temperature values. Each 10th second I have collected a temperature value, over a duration of several hours. I try to find the highest increase (Tslope) of the temperature within 6 temperature values. I tried the approach of rolling average but I was not even close to a solution. The data set looks like this:

R001 <- R001_G2_Q3_T

Test Subject      Datum                Zeit Temperatur Timepoint
1     G2    R001 2018-10-27 1899-12-31 16:51:45       38.4        Q3
2     G2    R001 2018-10-27 1899-12-31 16:51:55       38.4        Q3
3     G2    R001 2018-10-27 1899-12-31 16:52:05       38.4        Q3
4     G2    R001 2018-10-27 1899-12-31 16:52:15       38.4        Q3
5     G2    R001 2018-10-27 1899-12-31 16:52:25       38.4        Q3
6     G2    R001 2018-10-27 1899-12-31 16:52:35       38.3        Q3
7     G2    R001 2018-10-27 1899-12-31 16:52:45       38.4        Q3
8     G2    R001 2018-10-27 1899-12-31 16:52:55       38.5        Q3
9     G2    R001 2018-10-27 1899-12-31 16:53:06       38.3        Q3
10    G2    R001 2018-10-27 1899-12-31 16:53:16       38.4        Q3
11    G2    R001 2018-10-27 1899-12-31 16:53:26       38.4        Q3
12    G2    R001 2018-10-27 1899-12-31 16:53:36       38.4        Q3
13    G2    R001 2018-10-27 1899-12-31 16:53:46       38.4        Q3
14    G2    R001 2018-10-27 1899-12-31 16:53:56       38.4        Q3
15    G2    R001 2018-10-27 1899-12-31 16:54:06       38.3        Q3
16    G2    R001 2018-10-27 1899-12-31 16:54:16       38.3        Q3
17    G2    R001 2018-10-27 1899-12-31 16:54:26       38.3        Q3
18    G2    R001 2018-10-27 1899-12-31 16:54:36       38.4        Q3
19    G2    R001 2018-10-27 1899-12-31 16:54:47       38.4        Q3
20    G2    R001 2018-10-27 1899-12-31 16:54:57       38.3        Q3
21    G2    R001 2018-10-27 1899-12-31 16:55:07       38.4        Q3
22    G2    R001 2018-10-27 1899-12-31 16:55:17       38.4        Q3
23    G2    R001 2018-10-27 1899-12-31 16:55:27       38.4        Q3
24    G2    R001 2018-10-27 1899-12-31 16:55:37       38.4        Q3
25    G2    R001 2018-10-27 1899-12-31 16:55:47       38.5        Q3
26    G2    R001 2018-10-27 1899-12-31 16:55:57       38.4        Q3
27    G2    R001 2018-10-27 1899-12-31 16:56:07       38.4        Q3
28    G2    R001 2018-10-27 1899-12-31 16:56:17       38.4        Q3
29    G2    R001 2018-10-27 1899-12-31 16:56:28       38.5        Q3
30    G2    R001 2018-10-27 1899-12-31 16:56:38       38.4        Q3
31    G2    R001 2018-10-27 1899-12-31 16:56:48       38.4        Q3
32    G2    R001 2018-10-27 1899-12-31 16:56:58       38.5        Q3
33    G2    R001 2018-10-27 1899-12-31 16:57:08       38.5        Q3
[...]

What I expect is that I can say for example: "Between 16:55 and 16:56 was with 0.3°C the highest increase in core temperature"

Maybe it is easier to understand my problem, when I show you additionally an image: enter image description here

What I like to find is Tslope: Tslope will be calculated iteratively over a one minute period, including 6 successive measurement time points. Of course my data-set contains much more values that there will be a similar curve like in the image.

It would be great if someone could help me in this case. Thanks for your help.


Solution

  • I'm guessing this is what you are looking for:

    DT[, maxTslope_last6 := frollapply(Temperatur, n = 6, function(x) max(diff(x)))]
    
    > DT
        ID Test Subject      Datum                Zeit Temperatur Timepoint maxTslope_last6
     1:  1   G2    R001 2018-10-27 1899-12-31 16:51:45       38.4        Q3              NA
     2:  2   G2    R001 2018-10-27 1899-12-31 16:51:55       38.4        Q3              NA
     3:  3   G2    R001 2018-10-27 1899-12-31 16:52:05       38.4        Q3              NA
     4:  4   G2    R001 2018-10-27 1899-12-31 16:52:15       38.4        Q3              NA
     5:  5   G2    R001 2018-10-27 1899-12-31 16:52:25       38.4        Q3              NA
     6:  6   G2    R001 2018-10-27 1899-12-31 16:52:35       38.3        Q3             0.0
     7:  7   G2    R001 2018-10-27 1899-12-31 16:52:45       38.4        Q3             0.1
     8:  8   G2    R001 2018-10-27 1899-12-31 16:52:55       38.5        Q3             0.1
     9:  9   G2    R001 2018-10-27 1899-12-31 16:53:06       38.3        Q3             0.1
    10: 10   G2    R001 2018-10-27 1899-12-31 16:53:16       38.4        Q3             0.1
    11: 11   G2    R001 2018-10-27 1899-12-31 16:53:26       38.4        Q3             0.1
    12: 12   G2    R001 2018-10-27 1899-12-31 16:53:36       38.4        Q3             0.1
    13: 13   G2    R001 2018-10-27 1899-12-31 16:53:46       38.4        Q3             0.1
    14: 14   G2    R001 2018-10-27 1899-12-31 16:53:56       38.4        Q3             0.1
    15: 15   G2    R001 2018-10-27 1899-12-31 16:54:06       38.3        Q3             0.0
    16: 16   G2    R001 2018-10-27 1899-12-31 16:54:16       38.3        Q3             0.0
    17: 17   G2    R001 2018-10-27 1899-12-31 16:54:26       38.3        Q3             0.0
    18: 18   G2    R001 2018-10-27 1899-12-31 16:54:36       38.4        Q3             0.1
    19: 19   G2    R001 2018-10-27 1899-12-31 16:54:47       38.4        Q3             0.1
    20: 20   G2    R001 2018-10-27 1899-12-31 16:54:57       38.3        Q3             0.1
    21: 21   G2    R001 2018-10-27 1899-12-31 16:55:07       38.4        Q3             0.1
    22: 22   G2    R001 2018-10-27 1899-12-31 16:55:17       38.4        Q3             0.1
    23: 23   G2    R001 2018-10-27 1899-12-31 16:55:27       38.4        Q3             0.1
    24: 24   G2    R001 2018-10-27 1899-12-31 16:55:37       38.4        Q3             0.1
    25: 25   G2    R001 2018-10-27 1899-12-31 16:55:47       38.5        Q3             0.1
    26: 26   G2    R001 2018-10-27 1899-12-31 16:55:57       38.4        Q3             0.1
    27: 27   G2    R001 2018-10-27 1899-12-31 16:56:07       38.4        Q3             0.1
    28: 28   G2    R001 2018-10-27 1899-12-31 16:56:17       38.4        Q3             0.1
    29: 29   G2    R001 2018-10-27 1899-12-31 16:56:28       38.5        Q3             0.1
    30: 30   G2    R001 2018-10-27 1899-12-31 16:56:38       38.4        Q3             0.1
    31: 31   G2    R001 2018-10-27 1899-12-31 16:56:48       38.4        Q3             0.1
    32: 32   G2    R001 2018-10-27 1899-12-31 16:56:58       38.5        Q3             0.1
    33: 33   G2    R001 2018-10-27 1899-12-31 16:57:08       38.5        Q3             0.1
        ID Test Subject      Datum                Zeit Temperatur Timepoint maxTslope_last6