Home > Software engineering >  Rolling slope timedata
Rolling slope timedata

Time:11-24

Based on other posts, I can see the "normal" way to calculate running slope seems to be using a function and the rollapplyr command. Do we really not have a simpler built-in command for this?

I have a long dataset with x values being timestamps and y values being temperatures. Sensors being different groups of the data.

enter image description here

How would you tackle this? I would prefer to have temp change per minute. Do I need to convert timestamps to numeric and then calculate y2-y1/x2-x1?

CodePudding user response:

I usually would not assume that the time difference between two rows is always exactly the same. Let's be on the save side and calculate the slope based on delta_y / delta_x:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

# example data
data <- tribble(
  ~Time, ~Sensors, ~Temp,
  "2021-11-08 14:27:54", "s1", 22.2,
  "2021-11-08 14:28:24", "s1", 22.2,
  "2021-11-08 14:28:54", "s1", 22.1,
)

data %>%
  mutate(
    Time = parse_datetime(Time),
    Slope = (Temp - lag(Temp)) /
      as.numeric(difftime(Time, lag(Time), units = "mins"))
  )
#> # A tibble: 3 x 4
#>   Time                Sensors  Temp  Slope
#>   <dttm>              <chr>   <dbl>  <dbl>
#> 1 2021-11-08 14:27:54 s1       22.2 NA    
#> 2 2021-11-08 14:28:24 s1       22.2  0    
#> 3 2021-11-08 14:28:54 s1       22.1 -0.200

Created on 2021-11-24 by the reprex package (v2.0.1)

If the temp is measured in Celsius, the slope will be measured in Celsius per minute.

  • Related