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?
CodePudding user response:
You can use diff()
to calculate the difference between all consecutive elements.
df$dif <- as.integer(c(FALSE, abs(diff(df$Temp)) > 1.5))
df
# Timestamp Temp dif
# 1 2021-06-01 08:00:00 13.173 0
# 2 2021-06-01 08:15:00 13.269 0
# 3 2021-06-01 08:30:00 13.269 0
# 4 2021-06-01 08:45:00 13.269 0
# 5 2021-06-01 09:00:00 15.269 1
# 6 2021-06-01 09:15:00 13.269 1