'How to mark data points that are >X from the data point before it or after it

I have a date/time dataframe with values at every 15 minutes (there are hundreds of data points). I want to mark down values that are > or < 1.5 from the value before or after it.

This is my dataframe:

df = structure(list(Timestamp = 
                      structure(c(1622552400, 1622553300, 1622554200, 
                                  1622555100, 1622556000, 1622556900), 
                                class = c("POSIXct", "POSIXt"), tzone = "EST"), 
                    Temp = c(13.173, 13.269, 13.269, 13.269, 15.269, 13.269)), 
               row.names = c(NA, 6L), class = "data.frame")

I imagine I will need some sort of looping but I don't really know where to start with that.

Ideally, the result would look like a dataframe with a date/time column, the column of values, and a new column filled with 0's and 1's (where 0 signifies the value was not > or < 1.5 and 1 signifies the value was < or > 1.5). So something like this table here (which has all 0's because none of the values are > or < 1.5 from the value before it):

 Timestamp            Temp Difference_1.5
  <dttm>              <dbl>          <dbl>
1 2021-06-01 08:00:00  13.2              0
2 2021-06-01 08:15:00  13.3              0
3 2021-06-01 08:30:00  13.3              0
4 2021-06-01 08:45:00  13.3              0
5 2021-06-01 09:00:00  13.3              0
6 2021-06-01 09:15:00  13.3              0
> 

Any ideas out there?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source